| Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 1 | //===- InstructionCombining.cpp - Combine multiple instructions -----------===// | 
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // | 
| John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | //                     The LLVM Compiler Infrastructure | 
|  | 4 | // | 
|  | 5 | // This file was developed by the LLVM research group and is distributed under | 
|  | 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. | 
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // | 
| John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// | 
| Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 9 | // | 
|  | 10 | // InstructionCombining - Combine instructions to form fewer, simple | 
| Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 11 | // instructions.  This pass does not modify the CFG This pass is where algebraic | 
|  | 12 | // simplification happens. | 
| Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 13 | // | 
|  | 14 | // This pass combines things like: | 
| Chris Lattner | 0741842 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 15 | //    %Y = add i32 %X, 1 | 
|  | 16 | //    %Z = add i32 %Y, 1 | 
| Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 17 | // into: | 
| Chris Lattner | 0741842 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 18 | //    %Z = add i32 %X, 2 | 
| Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 19 | // | 
|  | 20 | // This is a simple worklist driven algorithm. | 
|  | 21 | // | 
| Chris Lattner | 216c7b8 | 2003-09-10 05:29:43 +0000 | [diff] [blame] | 22 | // This pass guarantees that the following canonicalizations are performed on | 
| Chris Lattner | bfb1d03 | 2003-07-23 21:41:57 +0000 | [diff] [blame] | 23 | // the program: | 
|  | 24 | //    1. If a binary operator has a constant operand, it is moved to the RHS | 
| Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 25 | //    2. Bitwise operators with constant operands are always grouped so that | 
|  | 26 | //       shifts are performed first, then or's, then and's, then xor's. | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 27 | //    3. Compare instructions are converted from <,>,<=,>= to ==,!= if possible | 
|  | 28 | //    4. All cmp instructions on boolean values are replaced with logical ops | 
| Chris Lattner | ede3fe0 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 29 | //    5. add X, X is represented as (X*2) => (X << 1) | 
|  | 30 | //    6. Multiplies with a power-of-two constant argument are transformed into | 
|  | 31 | //       shifts. | 
| Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 32 | //   ... etc. | 
| Chris Lattner | bfb1d03 | 2003-07-23 21:41:57 +0000 | [diff] [blame] | 33 | // | 
| Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 34 | //===----------------------------------------------------------------------===// | 
|  | 35 |  | 
| Chris Lattner | 7d2a539 | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 36 | #define DEBUG_TYPE "instcombine" | 
| Chris Lattner | b4cfa7f | 2002-05-07 20:03:00 +0000 | [diff] [blame] | 37 | #include "llvm/Transforms/Scalar.h" | 
| Chris Lattner | 00648e1 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 38 | #include "llvm/IntrinsicInst.h" | 
| Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 39 | #include "llvm/Pass.h" | 
| Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 40 | #include "llvm/DerivedTypes.h" | 
| Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 41 | #include "llvm/GlobalVariable.h" | 
| Chris Lattner | 024f4ab | 2007-01-30 23:46:24 +0000 | [diff] [blame] | 42 | #include "llvm/Analysis/ConstantFolding.h" | 
| Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 43 | #include "llvm/Target/TargetData.h" | 
|  | 44 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" | 
|  | 45 | #include "llvm/Transforms/Utils/Local.h" | 
| Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 46 | #include "llvm/Support/CallSite.h" | 
| Chris Lattner | 39c98bb | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 47 | #include "llvm/Support/Debug.h" | 
| Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 48 | #include "llvm/Support/GetElementPtrTypeIterator.h" | 
| Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 49 | #include "llvm/Support/InstVisitor.h" | 
| Chris Lattner | 22d00a8 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 50 | #include "llvm/Support/MathExtras.h" | 
| Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 51 | #include "llvm/Support/PatternMatch.h" | 
| Chris Lattner | 3d27be1 | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 52 | #include "llvm/Support/Compiler.h" | 
| Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 53 | #include "llvm/ADT/DenseMap.h" | 
| Chris Lattner | f96f4a8 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 54 | #include "llvm/ADT/SmallVector.h" | 
| Chris Lattner | 7907e5f | 2007-02-15 19:41:52 +0000 | [diff] [blame] | 55 | #include "llvm/ADT/SmallPtrSet.h" | 
| Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 56 | #include "llvm/ADT/Statistic.h" | 
| Chris Lattner | 39c98bb | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 57 | #include "llvm/ADT/STLExtras.h" | 
| Chris Lattner | 053c093 | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 58 | #include <algorithm> | 
| Reid Spencer | 755d0e7 | 2007-03-26 17:44:01 +0000 | [diff] [blame] | 59 | #include <sstream> | 
| Chris Lattner | 8427bff | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 60 | using namespace llvm; | 
| Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 61 | using namespace llvm::PatternMatch; | 
| Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 62 |  | 
| Chris Lattner | 79a42ac | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 63 | STATISTIC(NumCombined , "Number of insts combined"); | 
|  | 64 | STATISTIC(NumConstProp, "Number of constant folds"); | 
|  | 65 | STATISTIC(NumDeadInst , "Number of dead inst eliminated"); | 
|  | 66 | STATISTIC(NumDeadStore, "Number of dead stores eliminated"); | 
|  | 67 | STATISTIC(NumSunkInst , "Number of instructions sunk"); | 
| Chris Lattner | bf3a099 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 68 |  | 
| Chris Lattner | 79a42ac | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 69 | namespace { | 
| Chris Lattner | 4a4c7fe | 2006-06-28 22:08:15 +0000 | [diff] [blame] | 70 | class VISIBILITY_HIDDEN InstCombiner | 
|  | 71 | : public FunctionPass, | 
|  | 72 | public InstVisitor<InstCombiner, Instruction*> { | 
| Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 73 | // Worklist of all of the instructions that need to be simplified. | 
| Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 74 | std::vector<Instruction*> Worklist; | 
|  | 75 | DenseMap<Instruction*, unsigned> WorklistMap; | 
| Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 76 | TargetData *TD; | 
| Chris Lattner | 8258b44 | 2007-03-04 04:27:24 +0000 | [diff] [blame] | 77 | bool MustPreserveLCSSA; | 
| Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 78 | public: | 
|  | 79 | /// AddToWorkList - Add the specified instruction to the worklist if it | 
|  | 80 | /// isn't already in it. | 
|  | 81 | void AddToWorkList(Instruction *I) { | 
|  | 82 | if (WorklistMap.insert(std::make_pair(I, Worklist.size()))) | 
|  | 83 | Worklist.push_back(I); | 
|  | 84 | } | 
|  | 85 |  | 
|  | 86 | // RemoveFromWorkList - remove I from the worklist if it exists. | 
|  | 87 | void RemoveFromWorkList(Instruction *I) { | 
|  | 88 | DenseMap<Instruction*, unsigned>::iterator It = WorklistMap.find(I); | 
|  | 89 | if (It == WorklistMap.end()) return; // Not in worklist. | 
|  | 90 |  | 
|  | 91 | // Don't bother moving everything down, just null out the slot. | 
|  | 92 | Worklist[It->second] = 0; | 
|  | 93 |  | 
|  | 94 | WorklistMap.erase(It); | 
|  | 95 | } | 
|  | 96 |  | 
|  | 97 | Instruction *RemoveOneFromWorkList() { | 
|  | 98 | Instruction *I = Worklist.back(); | 
|  | 99 | Worklist.pop_back(); | 
|  | 100 | WorklistMap.erase(I); | 
|  | 101 | return I; | 
|  | 102 | } | 
| Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 103 |  | 
| Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 104 |  | 
| Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 105 | /// AddUsersToWorkList - When an instruction is simplified, add all users of | 
|  | 106 | /// the instruction to the work lists because they might get more simplified | 
|  | 107 | /// now. | 
|  | 108 | /// | 
| Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 109 | void AddUsersToWorkList(Value &I) { | 
| Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 110 | for (Value::use_iterator UI = I.use_begin(), UE = I.use_end(); | 
| Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 111 | UI != UE; ++UI) | 
| Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 112 | AddToWorkList(cast<Instruction>(*UI)); | 
| Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 113 | } | 
|  | 114 |  | 
| Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 115 | /// AddUsesToWorkList - When an instruction is simplified, add operands to | 
|  | 116 | /// the work lists because they might get more simplified now. | 
|  | 117 | /// | 
|  | 118 | void AddUsesToWorkList(Instruction &I) { | 
|  | 119 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) | 
|  | 120 | if (Instruction *Op = dyn_cast<Instruction>(I.getOperand(i))) | 
| Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 121 | AddToWorkList(Op); | 
| Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 122 | } | 
| Chris Lattner | 2deeaea | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 123 |  | 
|  | 124 | /// AddSoonDeadInstToWorklist - The specified instruction is about to become | 
|  | 125 | /// dead.  Add all of its operands to the worklist, turning them into | 
|  | 126 | /// undef's to reduce the number of uses of those instructions. | 
|  | 127 | /// | 
|  | 128 | /// Return the specified operand before it is turned into an undef. | 
|  | 129 | /// | 
|  | 130 | Value *AddSoonDeadInstToWorklist(Instruction &I, unsigned op) { | 
|  | 131 | Value *R = I.getOperand(op); | 
|  | 132 |  | 
|  | 133 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) | 
|  | 134 | if (Instruction *Op = dyn_cast<Instruction>(I.getOperand(i))) { | 
| Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 135 | AddToWorkList(Op); | 
| Chris Lattner | 2deeaea | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 136 | // Set the operand to undef to drop the use. | 
|  | 137 | I.setOperand(i, UndefValue::get(Op->getType())); | 
|  | 138 | } | 
|  | 139 |  | 
|  | 140 | return R; | 
|  | 141 | } | 
| Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 142 |  | 
| Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 143 | public: | 
| Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 144 | virtual bool runOnFunction(Function &F); | 
| Chris Lattner | 960a543 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 145 |  | 
|  | 146 | bool DoOneIteration(Function &F, unsigned ItNum); | 
| Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 147 |  | 
| Chris Lattner | f12cc84 | 2002-04-28 21:27:06 +0000 | [diff] [blame] | 148 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { | 
| Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 149 | AU.addRequired<TargetData>(); | 
| Owen Anderson | a6968f8 | 2006-07-10 19:03:49 +0000 | [diff] [blame] | 150 | AU.addPreservedID(LCSSAID); | 
| Chris Lattner | 820d971 | 2002-10-21 20:00:28 +0000 | [diff] [blame] | 151 | AU.setPreservesCFG(); | 
| Chris Lattner | f12cc84 | 2002-04-28 21:27:06 +0000 | [diff] [blame] | 152 | } | 
|  | 153 |  | 
| Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 154 | TargetData &getTargetData() const { return *TD; } | 
|  | 155 |  | 
| Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 156 | // Visitation implementation - Implement instruction combining for different | 
|  | 157 | // instruction types.  The semantics are as follows: | 
|  | 158 | // Return Value: | 
|  | 159 | //    null        - No change was made | 
| Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 160 | //     I          - Change was made, I is still valid, I may be dead though | 
| Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 161 | //   otherwise    - Change was made, replace I with returned instruction | 
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 162 | // | 
| Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 163 | Instruction *visitAdd(BinaryOperator &I); | 
|  | 164 | Instruction *visitSub(BinaryOperator &I); | 
|  | 165 | Instruction *visitMul(BinaryOperator &I); | 
| Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 166 | Instruction *visitURem(BinaryOperator &I); | 
|  | 167 | Instruction *visitSRem(BinaryOperator &I); | 
|  | 168 | Instruction *visitFRem(BinaryOperator &I); | 
|  | 169 | Instruction *commonRemTransforms(BinaryOperator &I); | 
|  | 170 | Instruction *commonIRemTransforms(BinaryOperator &I); | 
| Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 171 | Instruction *commonDivTransforms(BinaryOperator &I); | 
|  | 172 | Instruction *commonIDivTransforms(BinaryOperator &I); | 
|  | 173 | Instruction *visitUDiv(BinaryOperator &I); | 
|  | 174 | Instruction *visitSDiv(BinaryOperator &I); | 
|  | 175 | Instruction *visitFDiv(BinaryOperator &I); | 
| Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 176 | Instruction *visitAnd(BinaryOperator &I); | 
|  | 177 | Instruction *visitOr (BinaryOperator &I); | 
|  | 178 | Instruction *visitXor(BinaryOperator &I); | 
| Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 179 | Instruction *visitShl(BinaryOperator &I); | 
|  | 180 | Instruction *visitAShr(BinaryOperator &I); | 
|  | 181 | Instruction *visitLShr(BinaryOperator &I); | 
|  | 182 | Instruction *commonShiftTransforms(BinaryOperator &I); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 183 | Instruction *visitFCmpInst(FCmpInst &I); | 
|  | 184 | Instruction *visitICmpInst(ICmpInst &I); | 
|  | 185 | Instruction *visitICmpInstWithCastAndCast(ICmpInst &ICI); | 
| Chris Lattner | a74deaf | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 186 | Instruction *visitICmpInstWithInstAndIntCst(ICmpInst &ICI, | 
|  | 187 | Instruction *LHS, | 
|  | 188 | ConstantInt *RHS); | 
| Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 189 |  | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 190 | Instruction *FoldGEPICmp(User *GEPLHS, Value *RHS, | 
|  | 191 | ICmpInst::Predicate Cond, Instruction &I); | 
| Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 192 | Instruction *FoldShiftByConstant(Value *Op0, ConstantInt *Op1, | 
| Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 193 | BinaryOperator &I); | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 194 | Instruction *commonCastTransforms(CastInst &CI); | 
|  | 195 | Instruction *commonIntCastTransforms(CastInst &CI); | 
|  | 196 | Instruction *visitTrunc(CastInst &CI); | 
|  | 197 | Instruction *visitZExt(CastInst &CI); | 
|  | 198 | Instruction *visitSExt(CastInst &CI); | 
|  | 199 | Instruction *visitFPTrunc(CastInst &CI); | 
|  | 200 | Instruction *visitFPExt(CastInst &CI); | 
|  | 201 | Instruction *visitFPToUI(CastInst &CI); | 
|  | 202 | Instruction *visitFPToSI(CastInst &CI); | 
|  | 203 | Instruction *visitUIToFP(CastInst &CI); | 
|  | 204 | Instruction *visitSIToFP(CastInst &CI); | 
|  | 205 | Instruction *visitPtrToInt(CastInst &CI); | 
|  | 206 | Instruction *visitIntToPtr(CastInst &CI); | 
|  | 207 | Instruction *visitBitCast(CastInst &CI); | 
| Chris Lattner | 411336f | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 208 | Instruction *FoldSelectOpOp(SelectInst &SI, Instruction *TI, | 
|  | 209 | Instruction *FI); | 
| Chris Lattner | b909e8b | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 210 | Instruction *visitSelectInst(SelectInst &CI); | 
| Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 211 | Instruction *visitCallInst(CallInst &CI); | 
|  | 212 | Instruction *visitInvokeInst(InvokeInst &II); | 
| Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 213 | Instruction *visitPHINode(PHINode &PN); | 
|  | 214 | Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP); | 
| Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 215 | Instruction *visitAllocationInst(AllocationInst &AI); | 
| Chris Lattner | 8427bff | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 216 | Instruction *visitFreeInst(FreeInst &FI); | 
| Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 217 | Instruction *visitLoadInst(LoadInst &LI); | 
| Chris Lattner | 31f486c | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 218 | Instruction *visitStoreInst(StoreInst &SI); | 
| Chris Lattner | 9eef8a7 | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 219 | Instruction *visitBranchInst(BranchInst &BI); | 
| Chris Lattner | 4c9c20a | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 220 | Instruction *visitSwitchInst(SwitchInst &SI); | 
| Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 221 | Instruction *visitInsertElementInst(InsertElementInst &IE); | 
| Robert Bocchino | a835296 | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 222 | Instruction *visitExtractElementInst(ExtractElementInst &EI); | 
| Chris Lattner | fbb77a4 | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 223 | Instruction *visitShuffleVectorInst(ShuffleVectorInst &SVI); | 
| Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 224 |  | 
|  | 225 | // visitInstruction - Specify what to return for unhandled instructions... | 
| Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 226 | Instruction *visitInstruction(Instruction &I) { return 0; } | 
| Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 227 |  | 
| Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 228 | private: | 
| Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 229 | Instruction *visitCallSite(CallSite CS); | 
| Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 230 | bool transformConstExprCastCall(CallSite CS); | 
|  | 231 |  | 
| Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 232 | public: | 
| Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 233 | // InsertNewInstBefore - insert an instruction New before instruction Old | 
|  | 234 | // in the program.  Add the new instruction to the worklist. | 
|  | 235 | // | 
| Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 236 | Instruction *InsertNewInstBefore(Instruction *New, Instruction &Old) { | 
| Chris Lattner | 65217ff | 2002-08-23 18:32:43 +0000 | [diff] [blame] | 237 | assert(New && New->getParent() == 0 && | 
|  | 238 | "New instruction already inserted into a basic block!"); | 
| Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 239 | BasicBlock *BB = Old.getParent(); | 
|  | 240 | BB->getInstList().insert(&Old, New);  // Insert inst | 
| Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 241 | AddToWorkList(New); | 
| Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 242 | return New; | 
| Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 243 | } | 
|  | 244 |  | 
| Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 245 | /// InsertCastBefore - Insert a cast of V to TY before the instruction POS. | 
|  | 246 | /// This also adds the cast to the worklist.  Finally, this returns the | 
|  | 247 | /// cast. | 
| Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 248 | Value *InsertCastBefore(Instruction::CastOps opc, Value *V, const Type *Ty, | 
|  | 249 | Instruction &Pos) { | 
| Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 250 | if (V->getType() == Ty) return V; | 
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 251 |  | 
| Chris Lattner | e79d249 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 252 | if (Constant *CV = dyn_cast<Constant>(V)) | 
| Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 253 | return ConstantExpr::getCast(opc, CV, Ty); | 
| Chris Lattner | e79d249 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 254 |  | 
| Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 255 | Instruction *C = CastInst::create(opc, V, Ty, V->getName(), &Pos); | 
| Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 256 | AddToWorkList(C); | 
| Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 257 | return C; | 
|  | 258 | } | 
|  | 259 |  | 
| Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 260 | // ReplaceInstUsesWith - This method is to be used when an instruction is | 
|  | 261 | // found to be dead, replacable with another preexisting expression.  Here | 
|  | 262 | // we add all uses of I to the worklist, replace all uses of I with the new | 
|  | 263 | // value, then return I, so that the inst combiner will know that I was | 
|  | 264 | // modified. | 
|  | 265 | // | 
|  | 266 | Instruction *ReplaceInstUsesWith(Instruction &I, Value *V) { | 
| Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 267 | AddUsersToWorkList(I);         // Add all modified instrs to worklist | 
| Chris Lattner | 8953b90 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 268 | if (&I != V) { | 
|  | 269 | I.replaceAllUsesWith(V); | 
|  | 270 | return &I; | 
|  | 271 | } else { | 
|  | 272 | // If we are replacing the instruction with itself, this must be in a | 
|  | 273 | // segment of unreachable code, so just clobber the instruction. | 
| Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 274 | I.replaceAllUsesWith(UndefValue::get(I.getType())); | 
| Chris Lattner | 8953b90 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 275 | return &I; | 
|  | 276 | } | 
| Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 277 | } | 
| Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 278 |  | 
| Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 279 | // UpdateValueUsesWith - This method is to be used when an value is | 
|  | 280 | // found to be replacable with another preexisting expression or was | 
|  | 281 | // updated.  Here we add all uses of I to the worklist, replace all uses of | 
|  | 282 | // I with the new value (unless the instruction was just updated), then | 
|  | 283 | // return true, so that the inst combiner will know that I was modified. | 
|  | 284 | // | 
|  | 285 | bool UpdateValueUsesWith(Value *Old, Value *New) { | 
|  | 286 | AddUsersToWorkList(*Old);         // Add all modified instrs to worklist | 
|  | 287 | if (Old != New) | 
|  | 288 | Old->replaceAllUsesWith(New); | 
|  | 289 | if (Instruction *I = dyn_cast<Instruction>(Old)) | 
| Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 290 | AddToWorkList(I); | 
| Chris Lattner | 5b2edb1 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 291 | if (Instruction *I = dyn_cast<Instruction>(New)) | 
| Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 292 | AddToWorkList(I); | 
| Chris Lattner | 2590e51 | 2006-02-07 06:56:34 +0000 | [diff] [blame] | 293 | return true; | 
|  | 294 | } | 
|  | 295 |  | 
| Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 296 | // EraseInstFromFunction - When dealing with an instruction that has side | 
|  | 297 | // effects or produces a void value, we can't rely on DCE to delete the | 
|  | 298 | // instruction.  Instead, visit methods should return the value returned by | 
|  | 299 | // this function. | 
|  | 300 | Instruction *EraseInstFromFunction(Instruction &I) { | 
|  | 301 | assert(I.use_empty() && "Cannot erase instruction that is used!"); | 
|  | 302 | AddUsesToWorkList(I); | 
| Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 303 | RemoveFromWorkList(&I); | 
| Chris Lattner | 9530754 | 2004-11-18 21:41:39 +0000 | [diff] [blame] | 304 | I.eraseFromParent(); | 
| Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 305 | return 0;  // Don't do anything with FI | 
|  | 306 | } | 
|  | 307 |  | 
| Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 308 | private: | 
| Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 309 | /// InsertOperandCastBefore - This inserts a cast of V to DestTy before the | 
|  | 310 | /// InsertBefore instruction.  This is specialized a bit to avoid inserting | 
|  | 311 | /// casts that are known to not do anything... | 
|  | 312 | /// | 
| Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 313 | Value *InsertOperandCastBefore(Instruction::CastOps opcode, | 
|  | 314 | Value *V, const Type *DestTy, | 
| Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 315 | Instruction *InsertBefore); | 
|  | 316 |  | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 317 | /// SimplifyCommutative - This performs a few simplifications for | 
|  | 318 | /// commutative operators. | 
| Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 319 | bool SimplifyCommutative(BinaryOperator &I); | 
| Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 320 |  | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 321 | /// SimplifyCompare - This reorders the operands of a CmpInst to get them in | 
|  | 322 | /// most-complex to least-complex order. | 
|  | 323 | bool SimplifyCompare(CmpInst &I); | 
|  | 324 |  | 
| Reid Spencer | 959a21d | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 325 | /// SimplifyDemandedBits - Attempts to replace V with a simpler value based | 
|  | 326 | /// on the demanded bits. | 
| Reid Spencer | 1791f23 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 327 | bool SimplifyDemandedBits(Value *V, APInt DemandedMask, | 
|  | 328 | APInt& KnownZero, APInt& KnownOne, | 
|  | 329 | unsigned Depth = 0); | 
|  | 330 |  | 
| Chris Lattner | 2deeaea | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 331 | Value *SimplifyDemandedVectorElts(Value *V, uint64_t DemandedElts, | 
|  | 332 | uint64_t &UndefElts, unsigned Depth = 0); | 
|  | 333 |  | 
| Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 334 | // FoldOpIntoPhi - Given a binary operator or cast instruction which has a | 
|  | 335 | // PHI node as operand #0, see if we can fold the instruction into the PHI | 
|  | 336 | // (which is only possible if all operands to the PHI are constants). | 
|  | 337 | Instruction *FoldOpIntoPhi(Instruction &I); | 
|  | 338 |  | 
| Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 339 | // FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary" | 
|  | 340 | // operator and they all are only used by the PHI, PHI together their | 
|  | 341 | // inputs, and do the operation once, to the result of the PHI. | 
|  | 342 | Instruction *FoldPHIArgOpIntoPHI(PHINode &PN); | 
| Chris Lattner | cadac0c | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 343 | Instruction *FoldPHIArgBinOpIntoPHI(PHINode &PN); | 
|  | 344 |  | 
|  | 345 |  | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 346 | Instruction *OptAndOp(Instruction *Op, ConstantInt *OpRHS, | 
|  | 347 | ConstantInt *AndRHS, BinaryOperator &TheAnd); | 
| Chris Lattner | af51757 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 348 |  | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 349 | Value *FoldLogicalPlusAnd(Value *LHS, Value *RHS, ConstantInt *Mask, | 
| Chris Lattner | af51757 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 350 | bool isSub, Instruction &I); | 
| Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 351 | Instruction *InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 352 | bool isSigned, bool Inside, Instruction &IB); | 
| Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 353 | Instruction *PromoteCastOfAllocation(CastInst &CI, AllocationInst &AI); | 
| Chris Lattner | c482a9e | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 354 | Instruction *MatchBSwap(BinaryOperator &I); | 
|  | 355 |  | 
| Reid Spencer | 74a528b | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 356 | Value *EvaluateInDifferentType(Value *V, const Type *Ty, bool isSigned); | 
| Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 357 | }; | 
| Chris Lattner | b28b680 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 358 |  | 
| Chris Lattner | c2d3d31 | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 359 | RegisterPass<InstCombiner> X("instcombine", "Combine redundant instructions"); | 
| Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 360 | } | 
|  | 361 |  | 
| Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 362 | // getComplexity:  Assign a complexity or rank value to LLVM Values... | 
| Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 363 | //   0 -> undef, 1 -> Const, 2 -> Other, 3 -> Arg, 3 -> Unary, 4 -> OtherInst | 
| Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 364 | static unsigned getComplexity(Value *V) { | 
|  | 365 | if (isa<Instruction>(V)) { | 
|  | 366 | if (BinaryOperator::isNeg(V) || BinaryOperator::isNot(V)) | 
| Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 367 | return 3; | 
|  | 368 | return 4; | 
| Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 369 | } | 
| Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 370 | if (isa<Argument>(V)) return 3; | 
|  | 371 | return isa<Constant>(V) ? (isa<UndefValue>(V) ? 0 : 1) : 2; | 
| Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 372 | } | 
| Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 373 |  | 
| Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 374 | // isOnlyUse - Return true if this instruction will be deleted if we stop using | 
|  | 375 | // it. | 
|  | 376 | static bool isOnlyUse(Value *V) { | 
| Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 377 | return V->hasOneUse() || isa<Constant>(V); | 
| Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 378 | } | 
|  | 379 |  | 
| Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 380 | // getPromotedType - Return the specified type promoted as it would be to pass | 
|  | 381 | // though a va_arg area... | 
|  | 382 | static const Type *getPromotedType(const Type *Ty) { | 
| Reid Spencer | 7a9c62b | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 383 | if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty)) { | 
|  | 384 | if (ITy->getBitWidth() < 32) | 
|  | 385 | return Type::Int32Ty; | 
|  | 386 | } else if (Ty == Type::FloatTy) | 
|  | 387 | return Type::DoubleTy; | 
|  | 388 | return Ty; | 
| Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 389 | } | 
|  | 390 |  | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 391 | /// getBitCastOperand - If the specified operand is a CastInst or a constant | 
|  | 392 | /// expression bitcast,  return the operand value, otherwise return null. | 
|  | 393 | static Value *getBitCastOperand(Value *V) { | 
|  | 394 | if (BitCastInst *I = dyn_cast<BitCastInst>(V)) | 
| Chris Lattner | 567b81f | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 395 | return I->getOperand(0); | 
|  | 396 | else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 397 | if (CE->getOpcode() == Instruction::BitCast) | 
| Chris Lattner | 567b81f | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 398 | return CE->getOperand(0); | 
|  | 399 | return 0; | 
|  | 400 | } | 
|  | 401 |  | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 402 | /// This function is a wrapper around CastInst::isEliminableCastPair. It | 
|  | 403 | /// simply extracts arguments and returns what that function returns. | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 404 | static Instruction::CastOps | 
|  | 405 | isEliminableCastPair( | 
|  | 406 | const CastInst *CI, ///< The first cast instruction | 
|  | 407 | unsigned opcode,       ///< The opcode of the second cast instruction | 
|  | 408 | const Type *DstTy,     ///< The target type for the second cast instruction | 
|  | 409 | TargetData *TD         ///< The target data for pointer size | 
|  | 410 | ) { | 
|  | 411 |  | 
|  | 412 | const Type *SrcTy = CI->getOperand(0)->getType();   // A from above | 
|  | 413 | const Type *MidTy = CI->getType();                  // B from above | 
| Chris Lattner | 1d441ad | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 414 |  | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 415 | // Get the opcodes of the two Cast instructions | 
|  | 416 | Instruction::CastOps firstOp = Instruction::CastOps(CI->getOpcode()); | 
|  | 417 | Instruction::CastOps secondOp = Instruction::CastOps(opcode); | 
| Chris Lattner | 1d441ad | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 418 |  | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 419 | return Instruction::CastOps( | 
|  | 420 | CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy, | 
|  | 421 | DstTy, TD->getIntPtrType())); | 
| Chris Lattner | 1d441ad | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 422 | } | 
|  | 423 |  | 
|  | 424 | /// ValueRequiresCast - Return true if the cast from "V to Ty" actually results | 
|  | 425 | /// in any code being generated.  It does not require codegen if V is simple | 
|  | 426 | /// enough or if the cast can be folded into other casts. | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 427 | static bool ValueRequiresCast(Instruction::CastOps opcode, const Value *V, | 
|  | 428 | const Type *Ty, TargetData *TD) { | 
| Chris Lattner | 1d441ad | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 429 | if (V->getType() == Ty || isa<Constant>(V)) return false; | 
|  | 430 |  | 
| Chris Lattner | 99155be | 2006-05-25 23:24:33 +0000 | [diff] [blame] | 431 | // If this is another cast that can be eliminated, it isn't codegen either. | 
| Chris Lattner | 1d441ad | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 432 | if (const CastInst *CI = dyn_cast<CastInst>(V)) | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 433 | if (isEliminableCastPair(CI, opcode, Ty, TD)) | 
| Chris Lattner | 1d441ad | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 434 | return false; | 
|  | 435 | return true; | 
|  | 436 | } | 
|  | 437 |  | 
|  | 438 | /// InsertOperandCastBefore - This inserts a cast of V to DestTy before the | 
|  | 439 | /// InsertBefore instruction.  This is specialized a bit to avoid inserting | 
|  | 440 | /// casts that are known to not do anything... | 
|  | 441 | /// | 
| Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 442 | Value *InstCombiner::InsertOperandCastBefore(Instruction::CastOps opcode, | 
|  | 443 | Value *V, const Type *DestTy, | 
| Chris Lattner | 1d441ad | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 444 | Instruction *InsertBefore) { | 
|  | 445 | if (V->getType() == DestTy) return V; | 
|  | 446 | if (Constant *C = dyn_cast<Constant>(V)) | 
| Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 447 | return ConstantExpr::getCast(opcode, C, DestTy); | 
| Chris Lattner | 1d441ad | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 448 |  | 
| Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 449 | return InsertCastBefore(opcode, V, DestTy, *InsertBefore); | 
| Chris Lattner | 1d441ad | 2006-05-06 09:00:16 +0000 | [diff] [blame] | 450 | } | 
|  | 451 |  | 
| Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 452 | // SimplifyCommutative - This performs a few simplifications for commutative | 
|  | 453 | // operators: | 
| Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 454 | // | 
| Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 455 | //  1. Order operands such that they are listed from right (least complex) to | 
|  | 456 | //     left (most complex).  This puts constants before unary operators before | 
|  | 457 | //     binary operators. | 
|  | 458 | // | 
| Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 459 | //  2. Transform: (op (op V, C1), C2) ==> (op V, (op C1, C2)) | 
|  | 460 | //  3. Transform: (op (op V1, C1), (op V2, C2)) ==> (op (op V1, V2), (op C1,C2)) | 
| Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 461 | // | 
| Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 462 | bool InstCombiner::SimplifyCommutative(BinaryOperator &I) { | 
| Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 463 | bool Changed = false; | 
|  | 464 | if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1))) | 
|  | 465 | Changed = !I.swapOperands(); | 
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 466 |  | 
| Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 467 | if (!I.isAssociative()) return Changed; | 
|  | 468 | Instruction::BinaryOps Opcode = I.getOpcode(); | 
| Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 469 | if (BinaryOperator *Op = dyn_cast<BinaryOperator>(I.getOperand(0))) | 
|  | 470 | if (Op->getOpcode() == Opcode && isa<Constant>(Op->getOperand(1))) { | 
|  | 471 | if (isa<Constant>(I.getOperand(1))) { | 
| Chris Lattner | 3442844 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 472 | Constant *Folded = ConstantExpr::get(I.getOpcode(), | 
|  | 473 | cast<Constant>(I.getOperand(1)), | 
|  | 474 | cast<Constant>(Op->getOperand(1))); | 
| Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 475 | I.setOperand(0, Op->getOperand(0)); | 
|  | 476 | I.setOperand(1, Folded); | 
|  | 477 | return true; | 
|  | 478 | } else if (BinaryOperator *Op1=dyn_cast<BinaryOperator>(I.getOperand(1))) | 
|  | 479 | if (Op1->getOpcode() == Opcode && isa<Constant>(Op1->getOperand(1)) && | 
|  | 480 | isOnlyUse(Op) && isOnlyUse(Op1)) { | 
|  | 481 | Constant *C1 = cast<Constant>(Op->getOperand(1)); | 
|  | 482 | Constant *C2 = cast<Constant>(Op1->getOperand(1)); | 
|  | 483 |  | 
|  | 484 | // Fold (op (op V1, C1), (op V2, C2)) ==> (op (op V1, V2), (op C1,C2)) | 
| Chris Lattner | 3442844 | 2003-05-27 16:40:51 +0000 | [diff] [blame] | 485 | Constant *Folded = ConstantExpr::get(I.getOpcode(), C1, C2); | 
| Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 486 | Instruction *New = BinaryOperator::create(Opcode, Op->getOperand(0), | 
|  | 487 | Op1->getOperand(0), | 
|  | 488 | Op1->getName(), &I); | 
| Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 489 | AddToWorkList(New); | 
| Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 490 | I.setOperand(0, New); | 
|  | 491 | I.setOperand(1, Folded); | 
|  | 492 | return true; | 
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 493 | } | 
| Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 494 | } | 
| Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 495 | return Changed; | 
| Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 496 | } | 
| Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 497 |  | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 498 | /// SimplifyCompare - For a CmpInst this function just orders the operands | 
|  | 499 | /// so that theyare listed from right (least complex) to left (most complex). | 
|  | 500 | /// This puts constants before unary operators before binary operators. | 
|  | 501 | bool InstCombiner::SimplifyCompare(CmpInst &I) { | 
|  | 502 | if (getComplexity(I.getOperand(0)) >= getComplexity(I.getOperand(1))) | 
|  | 503 | return false; | 
|  | 504 | I.swapOperands(); | 
|  | 505 | // Compare instructions are not associative so there's nothing else we can do. | 
|  | 506 | return true; | 
|  | 507 | } | 
|  | 508 |  | 
| Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 509 | // dyn_castNegVal - Given a 'sub' instruction, return the RHS of the instruction | 
|  | 510 | // if the LHS is a constant zero (which is the 'negate' form). | 
| Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 511 | // | 
| Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 512 | static inline Value *dyn_castNegVal(Value *V) { | 
|  | 513 | if (BinaryOperator::isNeg(V)) | 
| Chris Lattner | d6f636a | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 514 | return BinaryOperator::getNegArgument(V); | 
| Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 515 |  | 
| Chris Lattner | 9ad0d55 | 2004-12-14 20:08:06 +0000 | [diff] [blame] | 516 | // Constants can be considered to be negated values if they can be folded. | 
|  | 517 | if (ConstantInt *C = dyn_cast<ConstantInt>(V)) | 
|  | 518 | return ConstantExpr::getNeg(C); | 
| Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 519 | return 0; | 
| Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 520 | } | 
|  | 521 |  | 
| Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 522 | static inline Value *dyn_castNotVal(Value *V) { | 
|  | 523 | if (BinaryOperator::isNot(V)) | 
| Chris Lattner | d6f636a | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 524 | return BinaryOperator::getNotArgument(V); | 
| Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 525 |  | 
|  | 526 | // Constants can be considered to be not'ed values... | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 527 | if (ConstantInt *C = dyn_cast<ConstantInt>(V)) | 
| Zhou Sheng | 9bc8ab1 | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 528 | return ConstantInt::get(~C->getValue()); | 
| Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 529 | return 0; | 
|  | 530 | } | 
|  | 531 |  | 
| Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 532 | // dyn_castFoldableMul - If this value is a multiply that can be folded into | 
|  | 533 | // other computations (because it has a constant operand), return the | 
| Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 534 | // non-constant operand of the multiply, and set CST to point to the multiplier. | 
|  | 535 | // Otherwise, return null. | 
| Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 536 | // | 
| Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 537 | static inline Value *dyn_castFoldableMul(Value *V, ConstantInt *&CST) { | 
| Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 538 | if (V->hasOneUse() && V->getType()->isInteger()) | 
| Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 539 | if (Instruction *I = dyn_cast<Instruction>(V)) { | 
| Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 540 | if (I->getOpcode() == Instruction::Mul) | 
| Chris Lattner | 97013636 | 2004-11-15 05:54:07 +0000 | [diff] [blame] | 541 | if ((CST = dyn_cast<ConstantInt>(I->getOperand(1)))) | 
| Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 542 | return I->getOperand(0); | 
| Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 543 | if (I->getOpcode() == Instruction::Shl) | 
| Chris Lattner | 97013636 | 2004-11-15 05:54:07 +0000 | [diff] [blame] | 544 | if ((CST = dyn_cast<ConstantInt>(I->getOperand(1)))) { | 
| Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 545 | // The multiplier is really 1 << CST. | 
| Zhou Sheng | 4961cf1 | 2007-03-29 01:57:21 +0000 | [diff] [blame] | 546 | uint32_t BitWidth = cast<IntegerType>(V->getType())->getBitWidth(); | 
| Zhou Sheng | b25806f | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 547 | uint32_t CSTVal = CST->getLimitedValue(BitWidth); | 
| Zhou Sheng | 4961cf1 | 2007-03-29 01:57:21 +0000 | [diff] [blame] | 548 | CST = ConstantInt::get(APInt(BitWidth, 1).shl(CSTVal)); | 
| Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 549 | return I->getOperand(0); | 
|  | 550 | } | 
|  | 551 | } | 
| Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 552 | return 0; | 
| Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 553 | } | 
| Chris Lattner | 31ae863 | 2002-08-14 17:51:49 +0000 | [diff] [blame] | 554 |  | 
| Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 555 | /// dyn_castGetElementPtr - If this is a getelementptr instruction or constant | 
|  | 556 | /// expression, return it. | 
|  | 557 | static User *dyn_castGetElementPtr(Value *V) { | 
|  | 558 | if (isa<GetElementPtrInst>(V)) return cast<User>(V); | 
|  | 559 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) | 
|  | 560 | if (CE->getOpcode() == Instruction::GetElementPtr) | 
|  | 561 | return cast<User>(V); | 
|  | 562 | return false; | 
|  | 563 | } | 
|  | 564 |  | 
| Reid Spencer | 80263aa | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 565 | /// AddOne - Add one to a ConstantInt | 
| Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 566 | static ConstantInt *AddOne(ConstantInt *C) { | 
| Reid Spencer | 624766f | 2007-03-25 19:55:33 +0000 | [diff] [blame] | 567 | APInt Val(C->getValue()); | 
|  | 568 | return ConstantInt::get(++Val); | 
| Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 569 | } | 
| Reid Spencer | 80263aa | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 570 | /// SubOne - Subtract one from a ConstantInt | 
| Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 571 | static ConstantInt *SubOne(ConstantInt *C) { | 
| Reid Spencer | 624766f | 2007-03-25 19:55:33 +0000 | [diff] [blame] | 572 | APInt Val(C->getValue()); | 
|  | 573 | return ConstantInt::get(--Val); | 
| Reid Spencer | 80263aa | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 574 | } | 
|  | 575 | /// Add - Add two ConstantInts together | 
|  | 576 | static ConstantInt *Add(ConstantInt *C1, ConstantInt *C2) { | 
|  | 577 | return ConstantInt::get(C1->getValue() + C2->getValue()); | 
|  | 578 | } | 
|  | 579 | /// And - Bitwise AND two ConstantInts together | 
|  | 580 | static ConstantInt *And(ConstantInt *C1, ConstantInt *C2) { | 
|  | 581 | return ConstantInt::get(C1->getValue() & C2->getValue()); | 
|  | 582 | } | 
|  | 583 | /// Subtract - Subtract one ConstantInt from another | 
|  | 584 | static ConstantInt *Subtract(ConstantInt *C1, ConstantInt *C2) { | 
|  | 585 | return ConstantInt::get(C1->getValue() - C2->getValue()); | 
|  | 586 | } | 
|  | 587 | /// Multiply - Multiply two ConstantInts together | 
|  | 588 | static ConstantInt *Multiply(ConstantInt *C1, ConstantInt *C2) { | 
|  | 589 | return ConstantInt::get(C1->getValue() * C2->getValue()); | 
| Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 590 | } | 
|  | 591 |  | 
| Chris Lattner | 4534dd59 | 2006-02-09 07:38:58 +0000 | [diff] [blame] | 592 | /// ComputeMaskedBits - Determine which of the bits specified in Mask are | 
|  | 593 | /// known to be either zero or one and return them in the KnownZero/KnownOne | 
| Reid Spencer | aa69640 | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 594 | /// bit sets.  This code only analyzes bits in Mask, in order to short-circuit | 
|  | 595 | /// processing. | 
|  | 596 | /// NOTE: we cannot consider 'undef' to be "IsZero" here.  The problem is that | 
|  | 597 | /// we cannot optimize based on the assumption that it is zero without changing | 
|  | 598 | /// it to be an explicit zero.  If we don't change it to zero, other code could | 
|  | 599 | /// optimized based on the contradictory assumption that it is non-zero. | 
|  | 600 | /// Because instcombine aggressively folds operations with undef args anyway, | 
|  | 601 | /// this won't lose us code quality. | 
| Reid Spencer | 5283032 | 2007-03-25 21:11:44 +0000 | [diff] [blame] | 602 | static void ComputeMaskedBits(Value *V, const APInt &Mask, APInt& KnownZero, | 
| Reid Spencer | aa69640 | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 603 | APInt& KnownOne, unsigned Depth = 0) { | 
| Zhou Sheng | af4341d | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 604 | assert(V && "No Value?"); | 
|  | 605 | assert(Depth <= 6 && "Limit Search Depth"); | 
| Reid Spencer | aa69640 | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 606 | uint32_t BitWidth = Mask.getBitWidth(); | 
| Zhou Sheng | 57e3f73 | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 607 | assert(cast<IntegerType>(V->getType())->getBitWidth() == BitWidth && | 
| Zhou Sheng | af4341d | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 608 | KnownZero.getBitWidth() == BitWidth && | 
| Reid Spencer | aa69640 | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 609 | KnownOne.getBitWidth() == BitWidth && | 
| Zhou Sheng | 57e3f73 | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 610 | "V, Mask, KnownOne and KnownZero should have same BitWidth"); | 
| Reid Spencer | aa69640 | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 611 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { | 
|  | 612 | // We know all of the bits for a constant! | 
| Zhou Sheng | af4341d | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 613 | KnownOne = CI->getValue() & Mask; | 
| Reid Spencer | aa69640 | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 614 | KnownZero = ~KnownOne & Mask; | 
|  | 615 | return; | 
|  | 616 | } | 
|  | 617 |  | 
| Reid Spencer | aa69640 | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 618 | if (Depth == 6 || Mask == 0) | 
|  | 619 | return;  // Limit search depth. | 
|  | 620 |  | 
|  | 621 | Instruction *I = dyn_cast<Instruction>(V); | 
|  | 622 | if (!I) return; | 
|  | 623 |  | 
| Zhou Sheng | af4341d | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 624 | KnownZero.clear(); KnownOne.clear();   // Don't know anything. | 
| Reid Spencer | aa69640 | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 625 | APInt KnownZero2(KnownZero), KnownOne2(KnownOne); | 
| Reid Spencer | aa69640 | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 626 |  | 
|  | 627 | switch (I->getOpcode()) { | 
| Reid Spencer | d8aad61 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 628 | case Instruction::And: { | 
| Reid Spencer | aa69640 | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 629 | // If either the LHS or the RHS are Zero, the result is zero. | 
|  | 630 | ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, Depth+1); | 
| Reid Spencer | d8aad61 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 631 | APInt Mask2(Mask & ~KnownZero); | 
|  | 632 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1); | 
| Reid Spencer | aa69640 | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 633 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); | 
|  | 634 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); | 
|  | 635 |  | 
|  | 636 | // Output known-1 bits are only known if set in both the LHS & RHS. | 
|  | 637 | KnownOne &= KnownOne2; | 
|  | 638 | // Output known-0 are known to be clear if zero in either the LHS | RHS. | 
|  | 639 | KnownZero |= KnownZero2; | 
|  | 640 | return; | 
| Reid Spencer | d8aad61 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 641 | } | 
|  | 642 | case Instruction::Or: { | 
| Reid Spencer | aa69640 | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 643 | ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, Depth+1); | 
| Reid Spencer | d8aad61 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 644 | APInt Mask2(Mask & ~KnownOne); | 
|  | 645 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1); | 
| Reid Spencer | aa69640 | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 646 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); | 
|  | 647 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); | 
|  | 648 |  | 
|  | 649 | // Output known-0 bits are only known if clear in both the LHS & RHS. | 
|  | 650 | KnownZero &= KnownZero2; | 
|  | 651 | // Output known-1 are known to be set if set in either the LHS | RHS. | 
|  | 652 | KnownOne |= KnownOne2; | 
|  | 653 | return; | 
| Reid Spencer | d8aad61 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 654 | } | 
| Reid Spencer | aa69640 | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 655 | case Instruction::Xor: { | 
|  | 656 | ComputeMaskedBits(I->getOperand(1), Mask, KnownZero, KnownOne, Depth+1); | 
|  | 657 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1); | 
|  | 658 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); | 
|  | 659 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); | 
|  | 660 |  | 
|  | 661 | // Output known-0 bits are known if clear or set in both the LHS & RHS. | 
|  | 662 | APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2); | 
|  | 663 | // Output known-1 are known to be set if set in only one of the LHS, RHS. | 
|  | 664 | KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2); | 
|  | 665 | KnownZero = KnownZeroOut; | 
|  | 666 | return; | 
|  | 667 | } | 
|  | 668 | case Instruction::Select: | 
|  | 669 | ComputeMaskedBits(I->getOperand(2), Mask, KnownZero, KnownOne, Depth+1); | 
|  | 670 | ComputeMaskedBits(I->getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1); | 
|  | 671 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); | 
|  | 672 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); | 
|  | 673 |  | 
|  | 674 | // Only known if known in both the LHS and RHS. | 
|  | 675 | KnownOne &= KnownOne2; | 
|  | 676 | KnownZero &= KnownZero2; | 
|  | 677 | return; | 
|  | 678 | case Instruction::FPTrunc: | 
|  | 679 | case Instruction::FPExt: | 
|  | 680 | case Instruction::FPToUI: | 
|  | 681 | case Instruction::FPToSI: | 
|  | 682 | case Instruction::SIToFP: | 
|  | 683 | case Instruction::PtrToInt: | 
|  | 684 | case Instruction::UIToFP: | 
|  | 685 | case Instruction::IntToPtr: | 
|  | 686 | return; // Can't work with floating point or pointers | 
| Zhou Sheng | af4341d | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 687 | case Instruction::Trunc: { | 
| Reid Spencer | aa69640 | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 688 | // All these have integer operands | 
| Zhou Sheng | af4341d | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 689 | uint32_t SrcBitWidth = | 
|  | 690 | cast<IntegerType>(I->getOperand(0)->getType())->getBitWidth(); | 
| Zhou Sheng | 57e3f73 | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 691 | APInt MaskIn(Mask); | 
|  | 692 | MaskIn.zext(SrcBitWidth); | 
|  | 693 | KnownZero.zext(SrcBitWidth); | 
|  | 694 | KnownOne.zext(SrcBitWidth); | 
|  | 695 | ComputeMaskedBits(I->getOperand(0), MaskIn, KnownZero, KnownOne, Depth+1); | 
| Zhou Sheng | af4341d | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 696 | KnownZero.trunc(BitWidth); | 
|  | 697 | KnownOne.trunc(BitWidth); | 
| Reid Spencer | aa69640 | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 698 | return; | 
| Zhou Sheng | af4341d | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 699 | } | 
| Reid Spencer | aa69640 | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 700 | case Instruction::BitCast: { | 
|  | 701 | const Type *SrcTy = I->getOperand(0)->getType(); | 
|  | 702 | if (SrcTy->isInteger()) { | 
|  | 703 | ComputeMaskedBits(I->getOperand(0), Mask, KnownZero, KnownOne, Depth+1); | 
|  | 704 | return; | 
|  | 705 | } | 
|  | 706 | break; | 
|  | 707 | } | 
|  | 708 | case Instruction::ZExt:  { | 
|  | 709 | // Compute the bits in the result that are not present in the input. | 
|  | 710 | const IntegerType *SrcTy = cast<IntegerType>(I->getOperand(0)->getType()); | 
| Zhou Sheng | af4341d | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 711 | uint32_t SrcBitWidth = SrcTy->getBitWidth(); | 
| Reid Spencer | cd99fbd | 2007-03-25 04:26:16 +0000 | [diff] [blame] | 712 |  | 
| Zhou Sheng | 57e3f73 | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 713 | APInt MaskIn(Mask); | 
|  | 714 | MaskIn.trunc(SrcBitWidth); | 
|  | 715 | KnownZero.trunc(SrcBitWidth); | 
|  | 716 | KnownOne.trunc(SrcBitWidth); | 
|  | 717 | ComputeMaskedBits(I->getOperand(0), MaskIn, KnownZero, KnownOne, Depth+1); | 
| Reid Spencer | aa69640 | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 718 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); | 
|  | 719 | // The top bits are known to be zero. | 
| Zhou Sheng | af4341d | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 720 | KnownZero.zext(BitWidth); | 
|  | 721 | KnownOne.zext(BitWidth); | 
| Zhou Sheng | 57e3f73 | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 722 | KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth); | 
| Reid Spencer | aa69640 | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 723 | return; | 
|  | 724 | } | 
|  | 725 | case Instruction::SExt: { | 
|  | 726 | // Compute the bits in the result that are not present in the input. | 
|  | 727 | const IntegerType *SrcTy = cast<IntegerType>(I->getOperand(0)->getType()); | 
| Zhou Sheng | af4341d | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 728 | uint32_t SrcBitWidth = SrcTy->getBitWidth(); | 
| Reid Spencer | cd99fbd | 2007-03-25 04:26:16 +0000 | [diff] [blame] | 729 |  | 
| Zhou Sheng | 57e3f73 | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 730 | APInt MaskIn(Mask); | 
|  | 731 | MaskIn.trunc(SrcBitWidth); | 
|  | 732 | KnownZero.trunc(SrcBitWidth); | 
|  | 733 | KnownOne.trunc(SrcBitWidth); | 
|  | 734 | ComputeMaskedBits(I->getOperand(0), MaskIn, KnownZero, KnownOne, Depth+1); | 
| Reid Spencer | aa69640 | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 735 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); | 
| Zhou Sheng | af4341d | 2007-03-13 02:23:10 +0000 | [diff] [blame] | 736 | KnownZero.zext(BitWidth); | 
|  | 737 | KnownOne.zext(BitWidth); | 
| Reid Spencer | aa69640 | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 738 |  | 
|  | 739 | // If the sign bit of the input is known set or clear, then we know the | 
|  | 740 | // top bits of the result. | 
| Zhou Sheng | 57e3f73 | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 741 | if (KnownZero[SrcBitWidth-1])             // Input sign bit known zero | 
| Zhou Sheng | 117477e | 2007-03-28 17:38:21 +0000 | [diff] [blame] | 742 | KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth); | 
| Zhou Sheng | 57e3f73 | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 743 | else if (KnownOne[SrcBitWidth-1])           // Input sign bit known set | 
| Zhou Sheng | 117477e | 2007-03-28 17:38:21 +0000 | [diff] [blame] | 744 | KnownOne |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth); | 
| Reid Spencer | aa69640 | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 745 | return; | 
|  | 746 | } | 
|  | 747 | case Instruction::Shl: | 
|  | 748 | // (shl X, C1) & C2 == 0   iff   (X & C2 >>u C1) == 0 | 
|  | 749 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { | 
| Zhou Sheng | b25806f | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 750 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth); | 
| Reid Spencer | d8aad61 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 751 | APInt Mask2(Mask.lshr(ShiftAmt)); | 
|  | 752 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero, KnownOne, Depth+1); | 
| Reid Spencer | aa69640 | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 753 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); | 
| Zhou Sheng | b3e00c4 | 2007-03-12 05:44:52 +0000 | [diff] [blame] | 754 | KnownZero <<= ShiftAmt; | 
|  | 755 | KnownOne  <<= ShiftAmt; | 
| Reid Spencer | 624766f | 2007-03-25 19:55:33 +0000 | [diff] [blame] | 756 | KnownZero |= APInt::getLowBitsSet(BitWidth, ShiftAmt); // low bits known 0 | 
| Reid Spencer | aa69640 | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 757 | return; | 
|  | 758 | } | 
|  | 759 | break; | 
|  | 760 | case Instruction::LShr: | 
|  | 761 | // (ushr X, C1) & C2 == 0   iff  (-1 >> C1) & C2 == 0 | 
|  | 762 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { | 
|  | 763 | // Compute the new bits that are at the top now. | 
| Zhou Sheng | b25806f | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 764 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth); | 
| Reid Spencer | aa69640 | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 765 |  | 
|  | 766 | // Unsigned shift right. | 
| Reid Spencer | d8aad61 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 767 | APInt Mask2(Mask.shl(ShiftAmt)); | 
|  | 768 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero,KnownOne,Depth+1); | 
| Reid Spencer | aa69640 | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 769 | assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); | 
|  | 770 | KnownZero = APIntOps::lshr(KnownZero, ShiftAmt); | 
|  | 771 | KnownOne  = APIntOps::lshr(KnownOne, ShiftAmt); | 
| Zhou Sheng | 57e3f73 | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 772 | // high bits known zero. | 
|  | 773 | KnownZero |= APInt::getHighBitsSet(BitWidth, ShiftAmt); | 
| Reid Spencer | aa69640 | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 774 | return; | 
|  | 775 | } | 
|  | 776 | break; | 
|  | 777 | case Instruction::AShr: | 
| Zhou Sheng | 57e3f73 | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 778 | // (ashr X, C1) & C2 == 0   iff  (-1 >> C1) & C2 == 0 | 
| Reid Spencer | aa69640 | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 779 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { | 
|  | 780 | // Compute the new bits that are at the top now. | 
| Zhou Sheng | b25806f | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 781 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth); | 
| Reid Spencer | aa69640 | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 782 |  | 
|  | 783 | // Signed shift right. | 
| Reid Spencer | d8aad61 | 2007-03-25 02:03:12 +0000 | [diff] [blame] | 784 | APInt Mask2(Mask.shl(ShiftAmt)); | 
|  | 785 | ComputeMaskedBits(I->getOperand(0), Mask2, KnownZero,KnownOne,Depth+1); | 
| Reid Spencer | aa69640 | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 786 | assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); | 
|  | 787 | KnownZero = APIntOps::lshr(KnownZero, ShiftAmt); | 
|  | 788 | KnownOne  = APIntOps::lshr(KnownOne, ShiftAmt); | 
|  | 789 |  | 
| Zhou Sheng | 57e3f73 | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 790 | APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt)); | 
|  | 791 | if (KnownZero[BitWidth-ShiftAmt-1])    // New bits are known zero. | 
| Reid Spencer | aa69640 | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 792 | KnownZero |= HighBits; | 
| Zhou Sheng | 57e3f73 | 2007-03-28 02:19:03 +0000 | [diff] [blame] | 793 | else if (KnownOne[BitWidth-ShiftAmt-1])  // New bits are known one. | 
| Reid Spencer | aa69640 | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 794 | KnownOne |= HighBits; | 
| Reid Spencer | aa69640 | 2007-03-08 01:46:38 +0000 | [diff] [blame] | 795 | return; | 
|  | 796 | } | 
|  | 797 | break; | 
|  | 798 | } | 
|  | 799 | } | 
|  | 800 |  | 
| Reid Spencer | bb5741f | 2007-03-08 01:52:58 +0000 | [diff] [blame] | 801 | /// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero.  We use | 
|  | 802 | /// this predicate to simplify operations downstream.  Mask is known to be zero | 
|  | 803 | /// for bits that V cannot have. | 
|  | 804 | static bool MaskedValueIsZero(Value *V, const APInt& Mask, unsigned Depth = 0) { | 
| Zhou Sheng | be171ee | 2007-03-12 16:54:56 +0000 | [diff] [blame] | 805 | APInt KnownZero(Mask.getBitWidth(), 0), KnownOne(Mask.getBitWidth(), 0); | 
| Reid Spencer | bb5741f | 2007-03-08 01:52:58 +0000 | [diff] [blame] | 806 | ComputeMaskedBits(V, Mask, KnownZero, KnownOne, Depth); | 
|  | 807 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); | 
|  | 808 | return (KnownZero & Mask) == Mask; | 
|  | 809 | } | 
|  | 810 |  | 
| Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 811 | /// ShrinkDemandedConstant - Check to see if the specified operand of the | 
|  | 812 | /// specified instruction is a constant integer.  If so, check to see if there | 
|  | 813 | /// are any bits set in the constant that are not demanded.  If so, shrink the | 
|  | 814 | /// constant and return true. | 
|  | 815 | static bool ShrinkDemandedConstant(Instruction *I, unsigned OpNo, | 
| Reid Spencer | d928178 | 2007-03-12 17:15:10 +0000 | [diff] [blame] | 816 | APInt Demanded) { | 
|  | 817 | assert(I && "No instruction?"); | 
|  | 818 | assert(OpNo < I->getNumOperands() && "Operand index too large"); | 
|  | 819 |  | 
|  | 820 | // If the operand is not a constant integer, nothing to do. | 
|  | 821 | ConstantInt *OpC = dyn_cast<ConstantInt>(I->getOperand(OpNo)); | 
|  | 822 | if (!OpC) return false; | 
|  | 823 |  | 
|  | 824 | // If there are no bits set that aren't demanded, nothing to do. | 
|  | 825 | Demanded.zextOrTrunc(OpC->getValue().getBitWidth()); | 
|  | 826 | if ((~Demanded & OpC->getValue()) == 0) | 
|  | 827 | return false; | 
|  | 828 |  | 
|  | 829 | // This instruction is producing bits that are not demanded. Shrink the RHS. | 
|  | 830 | Demanded &= OpC->getValue(); | 
|  | 831 | I->setOperand(OpNo, ConstantInt::get(Demanded)); | 
|  | 832 | return true; | 
|  | 833 | } | 
|  | 834 |  | 
| Chris Lattner | ee0f280 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 835 | // ComputeSignedMinMaxValuesFromKnownBits - Given a signed integer type and a | 
|  | 836 | // set of known zero and one bits, compute the maximum and minimum values that | 
|  | 837 | // could have the specified known zero and known one bits, returning them in | 
|  | 838 | // min/max. | 
|  | 839 | static void ComputeSignedMinMaxValuesFromKnownBits(const Type *Ty, | 
| Reid Spencer | c3e3b8a | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 840 | const APInt& KnownZero, | 
|  | 841 | const APInt& KnownOne, | 
|  | 842 | APInt& Min, APInt& Max) { | 
|  | 843 | uint32_t BitWidth = cast<IntegerType>(Ty)->getBitWidth(); | 
|  | 844 | assert(KnownZero.getBitWidth() == BitWidth && | 
|  | 845 | KnownOne.getBitWidth() == BitWidth && | 
|  | 846 | Min.getBitWidth() == BitWidth && Max.getBitWidth() == BitWidth && | 
|  | 847 | "Ty, KnownZero, KnownOne and Min, Max must have equal bitwidth."); | 
| Reid Spencer | cd99fbd | 2007-03-25 04:26:16 +0000 | [diff] [blame] | 848 | APInt UnknownBits = ~(KnownZero|KnownOne); | 
| Chris Lattner | ee0f280 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 849 |  | 
| Chris Lattner | ee0f280 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 850 | // The minimum value is when all unknown bits are zeros, EXCEPT for the sign | 
|  | 851 | // bit if it is unknown. | 
|  | 852 | Min = KnownOne; | 
|  | 853 | Max = KnownOne|UnknownBits; | 
|  | 854 |  | 
| Zhou Sheng | c2d3309 | 2007-03-28 05:15:57 +0000 | [diff] [blame] | 855 | if (UnknownBits[BitWidth-1]) { // Sign bit is unknown | 
| Zhou Sheng | 9bc8ab1 | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 856 | Min.set(BitWidth-1); | 
|  | 857 | Max.clear(BitWidth-1); | 
| Chris Lattner | ee0f280 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 858 | } | 
| Chris Lattner | ee0f280 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 859 | } | 
|  | 860 |  | 
|  | 861 | // ComputeUnsignedMinMaxValuesFromKnownBits - Given an unsigned integer type and | 
|  | 862 | // a set of known zero and one bits, compute the maximum and minimum values that | 
|  | 863 | // could have the specified known zero and known one bits, returning them in | 
|  | 864 | // min/max. | 
|  | 865 | static void ComputeUnsignedMinMaxValuesFromKnownBits(const Type *Ty, | 
| Reid Spencer | c3e3b8a | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 866 | const APInt& KnownZero, | 
|  | 867 | const APInt& KnownOne, | 
|  | 868 | APInt& Min, | 
|  | 869 | APInt& Max) { | 
|  | 870 | uint32_t BitWidth = cast<IntegerType>(Ty)->getBitWidth(); | 
|  | 871 | assert(KnownZero.getBitWidth() == BitWidth && | 
|  | 872 | KnownOne.getBitWidth() == BitWidth && | 
|  | 873 | Min.getBitWidth() == BitWidth && Max.getBitWidth() && | 
|  | 874 | "Ty, KnownZero, KnownOne and Min, Max must have equal bitwidth."); | 
| Reid Spencer | cd99fbd | 2007-03-25 04:26:16 +0000 | [diff] [blame] | 875 | APInt UnknownBits = ~(KnownZero|KnownOne); | 
| Chris Lattner | ee0f280 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 876 |  | 
|  | 877 | // The minimum value is when the unknown bits are all zeros. | 
|  | 878 | Min = KnownOne; | 
|  | 879 | // The maximum value is when the unknown bits are all ones. | 
|  | 880 | Max = KnownOne|UnknownBits; | 
|  | 881 | } | 
| Chris Lattner | 0157e7f | 2006-02-11 09:31:47 +0000 | [diff] [blame] | 882 |  | 
| Reid Spencer | 1791f23 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 883 | /// SimplifyDemandedBits - This function attempts to replace V with a simpler | 
|  | 884 | /// value based on the demanded bits. When this function is called, it is known | 
|  | 885 | /// that only the bits set in DemandedMask of the result of V are ever used | 
|  | 886 | /// downstream. Consequently, depending on the mask and V, it may be possible | 
|  | 887 | /// to replace V with a constant or one of its operands. In such cases, this | 
|  | 888 | /// function does the replacement and returns true. In all other cases, it | 
|  | 889 | /// returns false after analyzing the expression and setting KnownOne and known | 
|  | 890 | /// to be one in the expression. KnownZero contains all the bits that are known | 
|  | 891 | /// to be zero in the expression. These are provided to potentially allow the | 
|  | 892 | /// caller (which might recursively be SimplifyDemandedBits itself) to simplify | 
|  | 893 | /// the expression. KnownOne and KnownZero always follow the invariant that | 
|  | 894 | /// KnownOne & KnownZero == 0. That is, a bit can't be both 1 and 0. Note that | 
|  | 895 | /// the bits in KnownOne and KnownZero may only be accurate for those bits set | 
|  | 896 | /// in DemandedMask. Note also that the bitwidth of V, DemandedMask, KnownZero | 
|  | 897 | /// and KnownOne must all be the same. | 
|  | 898 | bool InstCombiner::SimplifyDemandedBits(Value *V, APInt DemandedMask, | 
|  | 899 | APInt& KnownZero, APInt& KnownOne, | 
|  | 900 | unsigned Depth) { | 
|  | 901 | assert(V != 0 && "Null pointer of Value???"); | 
|  | 902 | assert(Depth <= 6 && "Limit Search Depth"); | 
|  | 903 | uint32_t BitWidth = DemandedMask.getBitWidth(); | 
|  | 904 | const IntegerType *VTy = cast<IntegerType>(V->getType()); | 
|  | 905 | assert(VTy->getBitWidth() == BitWidth && | 
|  | 906 | KnownZero.getBitWidth() == BitWidth && | 
|  | 907 | KnownOne.getBitWidth() == BitWidth && | 
|  | 908 | "Value *V, DemandedMask, KnownZero and KnownOne \ | 
|  | 909 | must have same BitWidth"); | 
|  | 910 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { | 
|  | 911 | // We know all of the bits for a constant! | 
|  | 912 | KnownOne = CI->getValue() & DemandedMask; | 
|  | 913 | KnownZero = ~KnownOne & DemandedMask; | 
|  | 914 | return false; | 
|  | 915 | } | 
|  | 916 |  | 
| Zhou Sheng | b912844 | 2007-03-14 03:21:24 +0000 | [diff] [blame] | 917 | KnownZero.clear(); | 
|  | 918 | KnownOne.clear(); | 
| Reid Spencer | 1791f23 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 919 | if (!V->hasOneUse()) {    // Other users may use these bits. | 
|  | 920 | if (Depth != 0) {       // Not at the root. | 
|  | 921 | // Just compute the KnownZero/KnownOne bits to simplify things downstream. | 
|  | 922 | ComputeMaskedBits(V, DemandedMask, KnownZero, KnownOne, Depth); | 
|  | 923 | return false; | 
|  | 924 | } | 
|  | 925 | // If this is the root being simplified, allow it to have multiple uses, | 
|  | 926 | // just set the DemandedMask to all bits. | 
|  | 927 | DemandedMask = APInt::getAllOnesValue(BitWidth); | 
|  | 928 | } else if (DemandedMask == 0) {   // Not demanding any bits from V. | 
|  | 929 | if (V != UndefValue::get(VTy)) | 
|  | 930 | return UpdateValueUsesWith(V, UndefValue::get(VTy)); | 
|  | 931 | return false; | 
|  | 932 | } else if (Depth == 6) {        // Limit search depth. | 
|  | 933 | return false; | 
|  | 934 | } | 
|  | 935 |  | 
|  | 936 | Instruction *I = dyn_cast<Instruction>(V); | 
|  | 937 | if (!I) return false;        // Only analyze instructions. | 
|  | 938 |  | 
| Reid Spencer | 1791f23 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 939 | APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0); | 
|  | 940 | APInt &RHSKnownZero = KnownZero, &RHSKnownOne = KnownOne; | 
|  | 941 | switch (I->getOpcode()) { | 
|  | 942 | default: break; | 
|  | 943 | case Instruction::And: | 
|  | 944 | // If either the LHS or the RHS are Zero, the result is zero. | 
|  | 945 | if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, | 
|  | 946 | RHSKnownZero, RHSKnownOne, Depth+1)) | 
|  | 947 | return true; | 
|  | 948 | assert((RHSKnownZero & RHSKnownOne) == 0 && | 
|  | 949 | "Bits known to be one AND zero?"); | 
|  | 950 |  | 
|  | 951 | // If something is known zero on the RHS, the bits aren't demanded on the | 
|  | 952 | // LHS. | 
|  | 953 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask & ~RHSKnownZero, | 
|  | 954 | LHSKnownZero, LHSKnownOne, Depth+1)) | 
|  | 955 | return true; | 
|  | 956 | assert((LHSKnownZero & LHSKnownOne) == 0 && | 
|  | 957 | "Bits known to be one AND zero?"); | 
|  | 958 |  | 
|  | 959 | // If all of the demanded bits are known 1 on one side, return the other. | 
|  | 960 | // These bits cannot contribute to the result of the 'and'. | 
|  | 961 | if ((DemandedMask & ~LHSKnownZero & RHSKnownOne) == | 
|  | 962 | (DemandedMask & ~LHSKnownZero)) | 
|  | 963 | return UpdateValueUsesWith(I, I->getOperand(0)); | 
|  | 964 | if ((DemandedMask & ~RHSKnownZero & LHSKnownOne) == | 
|  | 965 | (DemandedMask & ~RHSKnownZero)) | 
|  | 966 | return UpdateValueUsesWith(I, I->getOperand(1)); | 
|  | 967 |  | 
|  | 968 | // If all of the demanded bits in the inputs are known zeros, return zero. | 
|  | 969 | if ((DemandedMask & (RHSKnownZero|LHSKnownZero)) == DemandedMask) | 
|  | 970 | return UpdateValueUsesWith(I, Constant::getNullValue(VTy)); | 
|  | 971 |  | 
|  | 972 | // If the RHS is a constant, see if we can simplify it. | 
|  | 973 | if (ShrinkDemandedConstant(I, 1, DemandedMask & ~LHSKnownZero)) | 
|  | 974 | return UpdateValueUsesWith(I, I); | 
|  | 975 |  | 
|  | 976 | // Output known-1 bits are only known if set in both the LHS & RHS. | 
|  | 977 | RHSKnownOne &= LHSKnownOne; | 
|  | 978 | // Output known-0 are known to be clear if zero in either the LHS | RHS. | 
|  | 979 | RHSKnownZero |= LHSKnownZero; | 
|  | 980 | break; | 
|  | 981 | case Instruction::Or: | 
|  | 982 | // If either the LHS or the RHS are One, the result is One. | 
|  | 983 | if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, | 
|  | 984 | RHSKnownZero, RHSKnownOne, Depth+1)) | 
|  | 985 | return true; | 
|  | 986 | assert((RHSKnownZero & RHSKnownOne) == 0 && | 
|  | 987 | "Bits known to be one AND zero?"); | 
|  | 988 | // If something is known one on the RHS, the bits aren't demanded on the | 
|  | 989 | // LHS. | 
|  | 990 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask & ~RHSKnownOne, | 
|  | 991 | LHSKnownZero, LHSKnownOne, Depth+1)) | 
|  | 992 | return true; | 
|  | 993 | assert((LHSKnownZero & LHSKnownOne) == 0 && | 
|  | 994 | "Bits known to be one AND zero?"); | 
|  | 995 |  | 
|  | 996 | // If all of the demanded bits are known zero on one side, return the other. | 
|  | 997 | // These bits cannot contribute to the result of the 'or'. | 
|  | 998 | if ((DemandedMask & ~LHSKnownOne & RHSKnownZero) == | 
|  | 999 | (DemandedMask & ~LHSKnownOne)) | 
|  | 1000 | return UpdateValueUsesWith(I, I->getOperand(0)); | 
|  | 1001 | if ((DemandedMask & ~RHSKnownOne & LHSKnownZero) == | 
|  | 1002 | (DemandedMask & ~RHSKnownOne)) | 
|  | 1003 | return UpdateValueUsesWith(I, I->getOperand(1)); | 
|  | 1004 |  | 
|  | 1005 | // If all of the potentially set bits on one side are known to be set on | 
|  | 1006 | // the other side, just use the 'other' side. | 
|  | 1007 | if ((DemandedMask & (~RHSKnownZero) & LHSKnownOne) == | 
|  | 1008 | (DemandedMask & (~RHSKnownZero))) | 
|  | 1009 | return UpdateValueUsesWith(I, I->getOperand(0)); | 
|  | 1010 | if ((DemandedMask & (~LHSKnownZero) & RHSKnownOne) == | 
|  | 1011 | (DemandedMask & (~LHSKnownZero))) | 
|  | 1012 | return UpdateValueUsesWith(I, I->getOperand(1)); | 
|  | 1013 |  | 
|  | 1014 | // If the RHS is a constant, see if we can simplify it. | 
|  | 1015 | if (ShrinkDemandedConstant(I, 1, DemandedMask)) | 
|  | 1016 | return UpdateValueUsesWith(I, I); | 
|  | 1017 |  | 
|  | 1018 | // Output known-0 bits are only known if clear in both the LHS & RHS. | 
|  | 1019 | RHSKnownZero &= LHSKnownZero; | 
|  | 1020 | // Output known-1 are known to be set if set in either the LHS | RHS. | 
|  | 1021 | RHSKnownOne |= LHSKnownOne; | 
|  | 1022 | break; | 
|  | 1023 | case Instruction::Xor: { | 
|  | 1024 | if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, | 
|  | 1025 | RHSKnownZero, RHSKnownOne, Depth+1)) | 
|  | 1026 | return true; | 
|  | 1027 | assert((RHSKnownZero & RHSKnownOne) == 0 && | 
|  | 1028 | "Bits known to be one AND zero?"); | 
|  | 1029 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, | 
|  | 1030 | LHSKnownZero, LHSKnownOne, Depth+1)) | 
|  | 1031 | return true; | 
|  | 1032 | assert((LHSKnownZero & LHSKnownOne) == 0 && | 
|  | 1033 | "Bits known to be one AND zero?"); | 
|  | 1034 |  | 
|  | 1035 | // If all of the demanded bits are known zero on one side, return the other. | 
|  | 1036 | // These bits cannot contribute to the result of the 'xor'. | 
|  | 1037 | if ((DemandedMask & RHSKnownZero) == DemandedMask) | 
|  | 1038 | return UpdateValueUsesWith(I, I->getOperand(0)); | 
|  | 1039 | if ((DemandedMask & LHSKnownZero) == DemandedMask) | 
|  | 1040 | return UpdateValueUsesWith(I, I->getOperand(1)); | 
|  | 1041 |  | 
|  | 1042 | // Output known-0 bits are known if clear or set in both the LHS & RHS. | 
|  | 1043 | APInt KnownZeroOut = (RHSKnownZero & LHSKnownZero) | | 
|  | 1044 | (RHSKnownOne & LHSKnownOne); | 
|  | 1045 | // Output known-1 are known to be set if set in only one of the LHS, RHS. | 
|  | 1046 | APInt KnownOneOut = (RHSKnownZero & LHSKnownOne) | | 
|  | 1047 | (RHSKnownOne & LHSKnownZero); | 
|  | 1048 |  | 
|  | 1049 | // If all of the demanded bits are known to be zero on one side or the | 
|  | 1050 | // other, turn this into an *inclusive* or. | 
|  | 1051 | //    e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0 | 
|  | 1052 | if ((DemandedMask & ~RHSKnownZero & ~LHSKnownZero) == 0) { | 
|  | 1053 | Instruction *Or = | 
|  | 1054 | BinaryOperator::createOr(I->getOperand(0), I->getOperand(1), | 
|  | 1055 | I->getName()); | 
|  | 1056 | InsertNewInstBefore(Or, *I); | 
|  | 1057 | return UpdateValueUsesWith(I, Or); | 
|  | 1058 | } | 
|  | 1059 |  | 
|  | 1060 | // If all of the demanded bits on one side are known, and all of the set | 
|  | 1061 | // bits on that side are also known to be set on the other side, turn this | 
|  | 1062 | // into an AND, as we know the bits will be cleared. | 
|  | 1063 | //    e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2 | 
|  | 1064 | if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask) { | 
|  | 1065 | // all known | 
|  | 1066 | if ((RHSKnownOne & LHSKnownOne) == RHSKnownOne) { | 
|  | 1067 | Constant *AndC = ConstantInt::get(~RHSKnownOne & DemandedMask); | 
|  | 1068 | Instruction *And = | 
|  | 1069 | BinaryOperator::createAnd(I->getOperand(0), AndC, "tmp"); | 
|  | 1070 | InsertNewInstBefore(And, *I); | 
|  | 1071 | return UpdateValueUsesWith(I, And); | 
|  | 1072 | } | 
|  | 1073 | } | 
|  | 1074 |  | 
|  | 1075 | // If the RHS is a constant, see if we can simplify it. | 
|  | 1076 | // FIXME: for XOR, we prefer to force bits to 1 if they will make a -1. | 
|  | 1077 | if (ShrinkDemandedConstant(I, 1, DemandedMask)) | 
|  | 1078 | return UpdateValueUsesWith(I, I); | 
|  | 1079 |  | 
|  | 1080 | RHSKnownZero = KnownZeroOut; | 
|  | 1081 | RHSKnownOne  = KnownOneOut; | 
|  | 1082 | break; | 
|  | 1083 | } | 
|  | 1084 | case Instruction::Select: | 
|  | 1085 | if (SimplifyDemandedBits(I->getOperand(2), DemandedMask, | 
|  | 1086 | RHSKnownZero, RHSKnownOne, Depth+1)) | 
|  | 1087 | return true; | 
|  | 1088 | if (SimplifyDemandedBits(I->getOperand(1), DemandedMask, | 
|  | 1089 | LHSKnownZero, LHSKnownOne, Depth+1)) | 
|  | 1090 | return true; | 
|  | 1091 | assert((RHSKnownZero & RHSKnownOne) == 0 && | 
|  | 1092 | "Bits known to be one AND zero?"); | 
|  | 1093 | assert((LHSKnownZero & LHSKnownOne) == 0 && | 
|  | 1094 | "Bits known to be one AND zero?"); | 
|  | 1095 |  | 
|  | 1096 | // If the operands are constants, see if we can simplify them. | 
|  | 1097 | if (ShrinkDemandedConstant(I, 1, DemandedMask)) | 
|  | 1098 | return UpdateValueUsesWith(I, I); | 
|  | 1099 | if (ShrinkDemandedConstant(I, 2, DemandedMask)) | 
|  | 1100 | return UpdateValueUsesWith(I, I); | 
|  | 1101 |  | 
|  | 1102 | // Only known if known in both the LHS and RHS. | 
|  | 1103 | RHSKnownOne &= LHSKnownOne; | 
|  | 1104 | RHSKnownZero &= LHSKnownZero; | 
|  | 1105 | break; | 
|  | 1106 | case Instruction::Trunc: { | 
|  | 1107 | uint32_t truncBf = | 
|  | 1108 | cast<IntegerType>(I->getOperand(0)->getType())->getBitWidth(); | 
| Zhou Sheng | a447557 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1109 | DemandedMask.zext(truncBf); | 
|  | 1110 | RHSKnownZero.zext(truncBf); | 
|  | 1111 | RHSKnownOne.zext(truncBf); | 
|  | 1112 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, | 
|  | 1113 | RHSKnownZero, RHSKnownOne, Depth+1)) | 
| Reid Spencer | 1791f23 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1114 | return true; | 
|  | 1115 | DemandedMask.trunc(BitWidth); | 
|  | 1116 | RHSKnownZero.trunc(BitWidth); | 
|  | 1117 | RHSKnownOne.trunc(BitWidth); | 
|  | 1118 | assert((RHSKnownZero & RHSKnownOne) == 0 && | 
|  | 1119 | "Bits known to be one AND zero?"); | 
|  | 1120 | break; | 
|  | 1121 | } | 
|  | 1122 | case Instruction::BitCast: | 
|  | 1123 | if (!I->getOperand(0)->getType()->isInteger()) | 
|  | 1124 | return false; | 
|  | 1125 |  | 
|  | 1126 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, | 
|  | 1127 | RHSKnownZero, RHSKnownOne, Depth+1)) | 
|  | 1128 | return true; | 
|  | 1129 | assert((RHSKnownZero & RHSKnownOne) == 0 && | 
|  | 1130 | "Bits known to be one AND zero?"); | 
|  | 1131 | break; | 
|  | 1132 | case Instruction::ZExt: { | 
|  | 1133 | // Compute the bits in the result that are not present in the input. | 
|  | 1134 | const IntegerType *SrcTy = cast<IntegerType>(I->getOperand(0)->getType()); | 
| Reid Spencer | cd99fbd | 2007-03-25 04:26:16 +0000 | [diff] [blame] | 1135 | uint32_t SrcBitWidth = SrcTy->getBitWidth(); | 
| Reid Spencer | 1791f23 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1136 |  | 
| Zhou Sheng | 444af49 | 2007-03-29 04:45:55 +0000 | [diff] [blame] | 1137 | DemandedMask.trunc(SrcBitWidth); | 
|  | 1138 | RHSKnownZero.trunc(SrcBitWidth); | 
|  | 1139 | RHSKnownOne.trunc(SrcBitWidth); | 
| Zhou Sheng | a447557 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1140 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMask, | 
|  | 1141 | RHSKnownZero, RHSKnownOne, Depth+1)) | 
| Reid Spencer | 1791f23 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1142 | return true; | 
|  | 1143 | DemandedMask.zext(BitWidth); | 
|  | 1144 | RHSKnownZero.zext(BitWidth); | 
|  | 1145 | RHSKnownOne.zext(BitWidth); | 
|  | 1146 | assert((RHSKnownZero & RHSKnownOne) == 0 && | 
|  | 1147 | "Bits known to be one AND zero?"); | 
|  | 1148 | // The top bits are known to be zero. | 
| Zhou Sheng | a447557 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1149 | RHSKnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth); | 
| Reid Spencer | 1791f23 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1150 | break; | 
|  | 1151 | } | 
|  | 1152 | case Instruction::SExt: { | 
|  | 1153 | // Compute the bits in the result that are not present in the input. | 
|  | 1154 | const IntegerType *SrcTy = cast<IntegerType>(I->getOperand(0)->getType()); | 
| Reid Spencer | cd99fbd | 2007-03-25 04:26:16 +0000 | [diff] [blame] | 1155 | uint32_t SrcBitWidth = SrcTy->getBitWidth(); | 
| Reid Spencer | 1791f23 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1156 |  | 
| Reid Spencer | 1791f23 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1157 | APInt InputDemandedBits = DemandedMask & | 
| Zhou Sheng | a447557 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1158 | APInt::getLowBitsSet(BitWidth, SrcBitWidth); | 
| Reid Spencer | 1791f23 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1159 |  | 
| Zhou Sheng | a447557 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1160 | APInt NewBits(APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth)); | 
| Reid Spencer | 1791f23 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1161 | // If any of the sign extended bits are demanded, we know that the sign | 
|  | 1162 | // bit is demanded. | 
|  | 1163 | if ((NewBits & DemandedMask) != 0) | 
| Zhou Sheng | 9bc8ab1 | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 1164 | InputDemandedBits.set(SrcBitWidth-1); | 
| Reid Spencer | 1791f23 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1165 |  | 
| Zhou Sheng | 444af49 | 2007-03-29 04:45:55 +0000 | [diff] [blame] | 1166 | InputDemandedBits.trunc(SrcBitWidth); | 
|  | 1167 | RHSKnownZero.trunc(SrcBitWidth); | 
|  | 1168 | RHSKnownOne.trunc(SrcBitWidth); | 
| Zhou Sheng | a447557 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1169 | if (SimplifyDemandedBits(I->getOperand(0), InputDemandedBits, | 
|  | 1170 | RHSKnownZero, RHSKnownOne, Depth+1)) | 
| Reid Spencer | 1791f23 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1171 | return true; | 
|  | 1172 | InputDemandedBits.zext(BitWidth); | 
|  | 1173 | RHSKnownZero.zext(BitWidth); | 
|  | 1174 | RHSKnownOne.zext(BitWidth); | 
|  | 1175 | assert((RHSKnownZero & RHSKnownOne) == 0 && | 
|  | 1176 | "Bits known to be one AND zero?"); | 
|  | 1177 |  | 
|  | 1178 | // If the sign bit of the input is known set or clear, then we know the | 
|  | 1179 | // top bits of the result. | 
|  | 1180 |  | 
|  | 1181 | // If the input sign bit is known zero, or if the NewBits are not demanded | 
|  | 1182 | // convert this into a zero extension. | 
| Zhou Sheng | a447557 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1183 | if (RHSKnownZero[SrcBitWidth-1] || (NewBits & ~DemandedMask) == NewBits) | 
| Reid Spencer | 1791f23 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1184 | { | 
|  | 1185 | // Convert to ZExt cast | 
|  | 1186 | CastInst *NewCast = new ZExtInst(I->getOperand(0), VTy, I->getName(), I); | 
|  | 1187 | return UpdateValueUsesWith(I, NewCast); | 
| Zhou Sheng | a447557 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1188 | } else if (RHSKnownOne[SrcBitWidth-1]) {    // Input sign bit known set | 
| Reid Spencer | 1791f23 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1189 | RHSKnownOne |= NewBits; | 
| Reid Spencer | 1791f23 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1190 | } | 
|  | 1191 | break; | 
|  | 1192 | } | 
|  | 1193 | case Instruction::Add: { | 
|  | 1194 | // Figure out what the input bits are.  If the top bits of the and result | 
|  | 1195 | // are not demanded, then the add doesn't demand them from its input | 
|  | 1196 | // either. | 
| Reid Spencer | 5283032 | 2007-03-25 21:11:44 +0000 | [diff] [blame] | 1197 | uint32_t NLZ = DemandedMask.countLeadingZeros(); | 
| Reid Spencer | 1791f23 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1198 |  | 
|  | 1199 | // If there is a constant on the RHS, there are a variety of xformations | 
|  | 1200 | // we can do. | 
|  | 1201 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) { | 
|  | 1202 | // If null, this should be simplified elsewhere.  Some of the xforms here | 
|  | 1203 | // won't work if the RHS is zero. | 
|  | 1204 | if (RHS->isZero()) | 
|  | 1205 | break; | 
|  | 1206 |  | 
|  | 1207 | // If the top bit of the output is demanded, demand everything from the | 
|  | 1208 | // input.  Otherwise, we demand all the input bits except NLZ top bits. | 
| Zhou Sheng | a447557 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1209 | APInt InDemandedBits(APInt::getLowBitsSet(BitWidth, BitWidth - NLZ)); | 
| Reid Spencer | 1791f23 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1210 |  | 
|  | 1211 | // Find information about known zero/one bits in the input. | 
|  | 1212 | if (SimplifyDemandedBits(I->getOperand(0), InDemandedBits, | 
|  | 1213 | LHSKnownZero, LHSKnownOne, Depth+1)) | 
|  | 1214 | return true; | 
|  | 1215 |  | 
|  | 1216 | // If the RHS of the add has bits set that can't affect the input, reduce | 
|  | 1217 | // the constant. | 
|  | 1218 | if (ShrinkDemandedConstant(I, 1, InDemandedBits)) | 
|  | 1219 | return UpdateValueUsesWith(I, I); | 
|  | 1220 |  | 
|  | 1221 | // Avoid excess work. | 
|  | 1222 | if (LHSKnownZero == 0 && LHSKnownOne == 0) | 
|  | 1223 | break; | 
|  | 1224 |  | 
|  | 1225 | // Turn it into OR if input bits are zero. | 
|  | 1226 | if ((LHSKnownZero & RHS->getValue()) == RHS->getValue()) { | 
|  | 1227 | Instruction *Or = | 
|  | 1228 | BinaryOperator::createOr(I->getOperand(0), I->getOperand(1), | 
|  | 1229 | I->getName()); | 
|  | 1230 | InsertNewInstBefore(Or, *I); | 
|  | 1231 | return UpdateValueUsesWith(I, Or); | 
|  | 1232 | } | 
|  | 1233 |  | 
|  | 1234 | // We can say something about the output known-zero and known-one bits, | 
|  | 1235 | // depending on potential carries from the input constant and the | 
|  | 1236 | // unknowns.  For example if the LHS is known to have at most the 0x0F0F0 | 
|  | 1237 | // bits set and the RHS constant is 0x01001, then we know we have a known | 
|  | 1238 | // one mask of 0x00001 and a known zero mask of 0xE0F0E. | 
|  | 1239 |  | 
|  | 1240 | // To compute this, we first compute the potential carry bits.  These are | 
|  | 1241 | // the bits which may be modified.  I'm not aware of a better way to do | 
|  | 1242 | // this scan. | 
| Zhou Sheng | 4f16402 | 2007-03-31 02:38:39 +0000 | [diff] [blame] | 1243 | const APInt& RHSVal = RHS->getValue(); | 
|  | 1244 | APInt CarryBits((~LHSKnownZero + RHSVal) ^ (~LHSKnownZero ^ RHSVal)); | 
| Reid Spencer | 1791f23 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1245 |  | 
|  | 1246 | // Now that we know which bits have carries, compute the known-1/0 sets. | 
|  | 1247 |  | 
|  | 1248 | // Bits are known one if they are known zero in one operand and one in the | 
|  | 1249 | // other, and there is no input carry. | 
|  | 1250 | RHSKnownOne = ((LHSKnownZero & RHSVal) | | 
|  | 1251 | (LHSKnownOne & ~RHSVal)) & ~CarryBits; | 
|  | 1252 |  | 
|  | 1253 | // Bits are known zero if they are known zero in both operands and there | 
|  | 1254 | // is no input carry. | 
|  | 1255 | RHSKnownZero = LHSKnownZero & ~RHSVal & ~CarryBits; | 
|  | 1256 | } else { | 
|  | 1257 | // If the high-bits of this ADD are not demanded, then it does not demand | 
|  | 1258 | // the high bits of its LHS or RHS. | 
| Zhou Sheng | a447557 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1259 | if (DemandedMask[BitWidth-1] == 0) { | 
| Reid Spencer | 1791f23 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1260 | // Right fill the mask of bits for this ADD to demand the most | 
|  | 1261 | // significant bit and all those below it. | 
| Zhou Sheng | a447557 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1262 | APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ)); | 
| Reid Spencer | 1791f23 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1263 | if (SimplifyDemandedBits(I->getOperand(0), DemandedFromOps, | 
|  | 1264 | LHSKnownZero, LHSKnownOne, Depth+1)) | 
|  | 1265 | return true; | 
|  | 1266 | if (SimplifyDemandedBits(I->getOperand(1), DemandedFromOps, | 
|  | 1267 | LHSKnownZero, LHSKnownOne, Depth+1)) | 
|  | 1268 | return true; | 
|  | 1269 | } | 
|  | 1270 | } | 
|  | 1271 | break; | 
|  | 1272 | } | 
|  | 1273 | case Instruction::Sub: | 
|  | 1274 | // If the high-bits of this SUB are not demanded, then it does not demand | 
|  | 1275 | // the high bits of its LHS or RHS. | 
| Zhou Sheng | a447557 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1276 | if (DemandedMask[BitWidth-1] == 0) { | 
| Reid Spencer | 1791f23 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1277 | // Right fill the mask of bits for this SUB to demand the most | 
|  | 1278 | // significant bit and all those below it. | 
| Zhou Sheng | 56cda95 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 1279 | uint32_t NLZ = DemandedMask.countLeadingZeros(); | 
| Zhou Sheng | a447557 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1280 | APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ)); | 
| Reid Spencer | 1791f23 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1281 | if (SimplifyDemandedBits(I->getOperand(0), DemandedFromOps, | 
|  | 1282 | LHSKnownZero, LHSKnownOne, Depth+1)) | 
|  | 1283 | return true; | 
|  | 1284 | if (SimplifyDemandedBits(I->getOperand(1), DemandedFromOps, | 
|  | 1285 | LHSKnownZero, LHSKnownOne, Depth+1)) | 
|  | 1286 | return true; | 
|  | 1287 | } | 
|  | 1288 | break; | 
|  | 1289 | case Instruction::Shl: | 
|  | 1290 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { | 
| Zhou Sheng | b25806f | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 1291 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth); | 
| Zhou Sheng | a447557 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1292 | APInt DemandedMaskIn(DemandedMask.lshr(ShiftAmt)); | 
|  | 1293 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMaskIn, | 
| Reid Spencer | 1791f23 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1294 | RHSKnownZero, RHSKnownOne, Depth+1)) | 
|  | 1295 | return true; | 
|  | 1296 | assert((RHSKnownZero & RHSKnownOne) == 0 && | 
|  | 1297 | "Bits known to be one AND zero?"); | 
|  | 1298 | RHSKnownZero <<= ShiftAmt; | 
|  | 1299 | RHSKnownOne  <<= ShiftAmt; | 
|  | 1300 | // low bits known zero. | 
| Zhou Sheng | d8c645b | 2007-03-14 09:07:33 +0000 | [diff] [blame] | 1301 | if (ShiftAmt) | 
| Zhou Sheng | 23f7a1c | 2007-03-28 15:02:20 +0000 | [diff] [blame] | 1302 | RHSKnownZero |= APInt::getLowBitsSet(BitWidth, ShiftAmt); | 
| Reid Spencer | 1791f23 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1303 | } | 
|  | 1304 | break; | 
|  | 1305 | case Instruction::LShr: | 
|  | 1306 | // For a logical shift right | 
|  | 1307 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { | 
| Zhou Sheng | b25806f | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 1308 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth); | 
| Reid Spencer | 1791f23 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1309 |  | 
| Reid Spencer | 1791f23 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1310 | // Unsigned shift right. | 
| Zhou Sheng | a447557 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1311 | APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt)); | 
|  | 1312 | if (SimplifyDemandedBits(I->getOperand(0), DemandedMaskIn, | 
| Reid Spencer | 1791f23 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1313 | RHSKnownZero, RHSKnownOne, Depth+1)) | 
|  | 1314 | return true; | 
|  | 1315 | assert((RHSKnownZero & RHSKnownOne) == 0 && | 
|  | 1316 | "Bits known to be one AND zero?"); | 
| Reid Spencer | 1791f23 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1317 | RHSKnownZero = APIntOps::lshr(RHSKnownZero, ShiftAmt); | 
|  | 1318 | RHSKnownOne  = APIntOps::lshr(RHSKnownOne, ShiftAmt); | 
| Zhou Sheng | d8c645b | 2007-03-14 09:07:33 +0000 | [diff] [blame] | 1319 | if (ShiftAmt) { | 
|  | 1320 | // Compute the new bits that are at the top now. | 
| Zhou Sheng | a447557 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1321 | APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt)); | 
| Zhou Sheng | d8c645b | 2007-03-14 09:07:33 +0000 | [diff] [blame] | 1322 | RHSKnownZero |= HighBits;  // high bits known zero. | 
|  | 1323 | } | 
| Reid Spencer | 1791f23 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1324 | } | 
|  | 1325 | break; | 
|  | 1326 | case Instruction::AShr: | 
|  | 1327 | // If this is an arithmetic shift right and only the low-bit is set, we can | 
|  | 1328 | // always convert this into a logical shr, even if the shift amount is | 
|  | 1329 | // variable.  The low bit of the shift cannot be an input sign bit unless | 
|  | 1330 | // the shift amount is >= the size of the datatype, which is undefined. | 
|  | 1331 | if (DemandedMask == 1) { | 
|  | 1332 | // Perform the logical shift right. | 
|  | 1333 | Value *NewVal = BinaryOperator::createLShr( | 
|  | 1334 | I->getOperand(0), I->getOperand(1), I->getName()); | 
|  | 1335 | InsertNewInstBefore(cast<Instruction>(NewVal), *I); | 
|  | 1336 | return UpdateValueUsesWith(I, NewVal); | 
|  | 1337 | } | 
|  | 1338 |  | 
|  | 1339 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { | 
| Zhou Sheng | fd28a33 | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 1340 | uint32_t ShiftAmt = SA->getLimitedValue(BitWidth); | 
| Reid Spencer | 1791f23 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1341 |  | 
| Reid Spencer | 1791f23 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1342 | // Signed shift right. | 
| Zhou Sheng | a447557 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1343 | APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt)); | 
| Reid Spencer | 1791f23 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1344 | if (SimplifyDemandedBits(I->getOperand(0), | 
| Zhou Sheng | a447557 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1345 | DemandedMaskIn, | 
| Reid Spencer | 1791f23 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1346 | RHSKnownZero, RHSKnownOne, Depth+1)) | 
|  | 1347 | return true; | 
|  | 1348 | assert((RHSKnownZero & RHSKnownOne) == 0 && | 
|  | 1349 | "Bits known to be one AND zero?"); | 
|  | 1350 | // Compute the new bits that are at the top now. | 
| Zhou Sheng | a447557 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1351 | APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt)); | 
| Reid Spencer | 1791f23 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1352 | RHSKnownZero = APIntOps::lshr(RHSKnownZero, ShiftAmt); | 
|  | 1353 | RHSKnownOne  = APIntOps::lshr(RHSKnownOne, ShiftAmt); | 
|  | 1354 |  | 
|  | 1355 | // Handle the sign bits. | 
|  | 1356 | APInt SignBit(APInt::getSignBit(BitWidth)); | 
|  | 1357 | // Adjust to where it is now in the mask. | 
|  | 1358 | SignBit = APIntOps::lshr(SignBit, ShiftAmt); | 
|  | 1359 |  | 
|  | 1360 | // If the input sign bit is known to be zero, or if none of the top bits | 
|  | 1361 | // are demanded, turn this into an unsigned shift right. | 
| Zhou Sheng | a447557 | 2007-03-29 02:26:30 +0000 | [diff] [blame] | 1362 | if (RHSKnownZero[BitWidth-ShiftAmt-1] || | 
| Reid Spencer | 1791f23 | 2007-03-12 17:25:59 +0000 | [diff] [blame] | 1363 | (HighBits & ~DemandedMask) == HighBits) { | 
|  | 1364 | // Perform the logical shift right. | 
|  | 1365 | Value *NewVal = BinaryOperator::createLShr( | 
|  | 1366 | I->getOperand(0), SA, I->getName()); | 
|  | 1367 | InsertNewInstBefore(cast<Instruction>(NewVal), *I); | 
|  | 1368 | return UpdateValueUsesWith(I, NewVal); | 
|  | 1369 | } else if ((RHSKnownOne & SignBit) != 0) { // New bits are known one. | 
|  | 1370 | RHSKnownOne |= HighBits; | 
|  | 1371 | } | 
|  | 1372 | } | 
|  | 1373 | break; | 
|  | 1374 | } | 
|  | 1375 |  | 
|  | 1376 | // If the client is only demanding bits that we know, return the known | 
|  | 1377 | // constant. | 
|  | 1378 | if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask) | 
|  | 1379 | return UpdateValueUsesWith(I, ConstantInt::get(RHSKnownOne)); | 
|  | 1380 | return false; | 
|  | 1381 | } | 
|  | 1382 |  | 
| Chris Lattner | 2deeaea | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1383 |  | 
|  | 1384 | /// SimplifyDemandedVectorElts - The specified value producecs a vector with | 
|  | 1385 | /// 64 or fewer elements.  DemandedElts contains the set of elements that are | 
|  | 1386 | /// actually used by the caller.  This method analyzes which elements of the | 
|  | 1387 | /// operand are undef and returns that information in UndefElts. | 
|  | 1388 | /// | 
|  | 1389 | /// If the information about demanded elements can be used to simplify the | 
|  | 1390 | /// operation, the operation is simplified, then the resultant value is | 
|  | 1391 | /// returned.  This returns null if no change was made. | 
|  | 1392 | Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, uint64_t DemandedElts, | 
|  | 1393 | uint64_t &UndefElts, | 
|  | 1394 | unsigned Depth) { | 
| Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1395 | unsigned VWidth = cast<VectorType>(V->getType())->getNumElements(); | 
| Chris Lattner | 2deeaea | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1396 | assert(VWidth <= 64 && "Vector too wide to analyze!"); | 
|  | 1397 | uint64_t EltMask = ~0ULL >> (64-VWidth); | 
|  | 1398 | assert(DemandedElts != EltMask && (DemandedElts & ~EltMask) == 0 && | 
|  | 1399 | "Invalid DemandedElts!"); | 
|  | 1400 |  | 
|  | 1401 | if (isa<UndefValue>(V)) { | 
|  | 1402 | // If the entire vector is undefined, just return this info. | 
|  | 1403 | UndefElts = EltMask; | 
|  | 1404 | return 0; | 
|  | 1405 | } else if (DemandedElts == 0) { // If nothing is demanded, provide undef. | 
|  | 1406 | UndefElts = EltMask; | 
|  | 1407 | return UndefValue::get(V->getType()); | 
|  | 1408 | } | 
|  | 1409 |  | 
|  | 1410 | UndefElts = 0; | 
| Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1411 | if (ConstantVector *CP = dyn_cast<ConstantVector>(V)) { | 
|  | 1412 | const Type *EltTy = cast<VectorType>(V->getType())->getElementType(); | 
| Chris Lattner | 2deeaea | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1413 | Constant *Undef = UndefValue::get(EltTy); | 
|  | 1414 |  | 
|  | 1415 | std::vector<Constant*> Elts; | 
|  | 1416 | for (unsigned i = 0; i != VWidth; ++i) | 
|  | 1417 | if (!(DemandedElts & (1ULL << i))) {   // If not demanded, set to undef. | 
|  | 1418 | Elts.push_back(Undef); | 
|  | 1419 | UndefElts |= (1ULL << i); | 
|  | 1420 | } else if (isa<UndefValue>(CP->getOperand(i))) {   // Already undef. | 
|  | 1421 | Elts.push_back(Undef); | 
|  | 1422 | UndefElts |= (1ULL << i); | 
|  | 1423 | } else {                               // Otherwise, defined. | 
|  | 1424 | Elts.push_back(CP->getOperand(i)); | 
|  | 1425 | } | 
|  | 1426 |  | 
|  | 1427 | // If we changed the constant, return it. | 
| Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1428 | Constant *NewCP = ConstantVector::get(Elts); | 
| Chris Lattner | 2deeaea | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1429 | return NewCP != CP ? NewCP : 0; | 
|  | 1430 | } else if (isa<ConstantAggregateZero>(V)) { | 
| Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1431 | // Simplify the CAZ to a ConstantVector where the non-demanded elements are | 
| Chris Lattner | 2deeaea | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1432 | // set to undef. | 
| Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1433 | const Type *EltTy = cast<VectorType>(V->getType())->getElementType(); | 
| Chris Lattner | 2deeaea | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1434 | Constant *Zero = Constant::getNullValue(EltTy); | 
|  | 1435 | Constant *Undef = UndefValue::get(EltTy); | 
|  | 1436 | std::vector<Constant*> Elts; | 
|  | 1437 | for (unsigned i = 0; i != VWidth; ++i) | 
|  | 1438 | Elts.push_back((DemandedElts & (1ULL << i)) ? Zero : Undef); | 
|  | 1439 | UndefElts = DemandedElts ^ EltMask; | 
| Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1440 | return ConstantVector::get(Elts); | 
| Chris Lattner | 2deeaea | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1441 | } | 
|  | 1442 |  | 
|  | 1443 | if (!V->hasOneUse()) {    // Other users may use these bits. | 
|  | 1444 | if (Depth != 0) {       // Not at the root. | 
|  | 1445 | // TODO: Just compute the UndefElts information recursively. | 
|  | 1446 | return false; | 
|  | 1447 | } | 
|  | 1448 | return false; | 
|  | 1449 | } else if (Depth == 10) {        // Limit search depth. | 
|  | 1450 | return false; | 
|  | 1451 | } | 
|  | 1452 |  | 
|  | 1453 | Instruction *I = dyn_cast<Instruction>(V); | 
|  | 1454 | if (!I) return false;        // Only analyze instructions. | 
|  | 1455 |  | 
|  | 1456 | bool MadeChange = false; | 
|  | 1457 | uint64_t UndefElts2; | 
|  | 1458 | Value *TmpV; | 
|  | 1459 | switch (I->getOpcode()) { | 
|  | 1460 | default: break; | 
|  | 1461 |  | 
|  | 1462 | case Instruction::InsertElement: { | 
|  | 1463 | // If this is a variable index, we don't know which element it overwrites. | 
|  | 1464 | // demand exactly the same input as we produce. | 
| Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1465 | ConstantInt *Idx = dyn_cast<ConstantInt>(I->getOperand(2)); | 
| Chris Lattner | 2deeaea | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1466 | if (Idx == 0) { | 
|  | 1467 | // Note that we can't propagate undef elt info, because we don't know | 
|  | 1468 | // which elt is getting updated. | 
|  | 1469 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, | 
|  | 1470 | UndefElts2, Depth+1); | 
|  | 1471 | if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; } | 
|  | 1472 | break; | 
|  | 1473 | } | 
|  | 1474 |  | 
|  | 1475 | // If this is inserting an element that isn't demanded, remove this | 
|  | 1476 | // insertelement. | 
| Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1477 | unsigned IdxNo = Idx->getZExtValue(); | 
| Chris Lattner | 2deeaea | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 1478 | if (IdxNo >= VWidth || (DemandedElts & (1ULL << IdxNo)) == 0) | 
|  | 1479 | return AddSoonDeadInstToWorklist(*I, 0); | 
|  | 1480 |  | 
|  | 1481 | // Otherwise, the element inserted overwrites whatever was there, so the | 
|  | 1482 | // input demanded set is simpler than the output set. | 
|  | 1483 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), | 
|  | 1484 | DemandedElts & ~(1ULL << IdxNo), | 
|  | 1485 | UndefElts, Depth+1); | 
|  | 1486 | if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; } | 
|  | 1487 |  | 
|  | 1488 | // The inserted element is defined. | 
|  | 1489 | UndefElts |= 1ULL << IdxNo; | 
|  | 1490 | break; | 
|  | 1491 | } | 
|  | 1492 |  | 
|  | 1493 | case Instruction::And: | 
|  | 1494 | case Instruction::Or: | 
|  | 1495 | case Instruction::Xor: | 
|  | 1496 | case Instruction::Add: | 
|  | 1497 | case Instruction::Sub: | 
|  | 1498 | case Instruction::Mul: | 
|  | 1499 | // div/rem demand all inputs, because they don't want divide by zero. | 
|  | 1500 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, | 
|  | 1501 | UndefElts, Depth+1); | 
|  | 1502 | if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; } | 
|  | 1503 | TmpV = SimplifyDemandedVectorElts(I->getOperand(1), DemandedElts, | 
|  | 1504 | UndefElts2, Depth+1); | 
|  | 1505 | if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; } | 
|  | 1506 |  | 
|  | 1507 | // Output elements are undefined if both are undefined.  Consider things | 
|  | 1508 | // like undef&0.  The result is known zero, not undef. | 
|  | 1509 | UndefElts &= UndefElts2; | 
|  | 1510 | break; | 
|  | 1511 |  | 
|  | 1512 | case Instruction::Call: { | 
|  | 1513 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(I); | 
|  | 1514 | if (!II) break; | 
|  | 1515 | switch (II->getIntrinsicID()) { | 
|  | 1516 | default: break; | 
|  | 1517 |  | 
|  | 1518 | // Binary vector operations that work column-wise.  A dest element is a | 
|  | 1519 | // function of the corresponding input elements from the two inputs. | 
|  | 1520 | case Intrinsic::x86_sse_sub_ss: | 
|  | 1521 | case Intrinsic::x86_sse_mul_ss: | 
|  | 1522 | case Intrinsic::x86_sse_min_ss: | 
|  | 1523 | case Intrinsic::x86_sse_max_ss: | 
|  | 1524 | case Intrinsic::x86_sse2_sub_sd: | 
|  | 1525 | case Intrinsic::x86_sse2_mul_sd: | 
|  | 1526 | case Intrinsic::x86_sse2_min_sd: | 
|  | 1527 | case Intrinsic::x86_sse2_max_sd: | 
|  | 1528 | TmpV = SimplifyDemandedVectorElts(II->getOperand(1), DemandedElts, | 
|  | 1529 | UndefElts, Depth+1); | 
|  | 1530 | if (TmpV) { II->setOperand(1, TmpV); MadeChange = true; } | 
|  | 1531 | TmpV = SimplifyDemandedVectorElts(II->getOperand(2), DemandedElts, | 
|  | 1532 | UndefElts2, Depth+1); | 
|  | 1533 | if (TmpV) { II->setOperand(2, TmpV); MadeChange = true; } | 
|  | 1534 |  | 
|  | 1535 | // If only the low elt is demanded and this is a scalarizable intrinsic, | 
|  | 1536 | // scalarize it now. | 
|  | 1537 | if (DemandedElts == 1) { | 
|  | 1538 | switch (II->getIntrinsicID()) { | 
|  | 1539 | default: break; | 
|  | 1540 | case Intrinsic::x86_sse_sub_ss: | 
|  | 1541 | case Intrinsic::x86_sse_mul_ss: | 
|  | 1542 | case Intrinsic::x86_sse2_sub_sd: | 
|  | 1543 | case Intrinsic::x86_sse2_mul_sd: | 
|  | 1544 | // TODO: Lower MIN/MAX/ABS/etc | 
|  | 1545 | Value *LHS = II->getOperand(1); | 
|  | 1546 | Value *RHS = II->getOperand(2); | 
|  | 1547 | // Extract the element as scalars. | 
|  | 1548 | LHS = InsertNewInstBefore(new ExtractElementInst(LHS, 0U,"tmp"), *II); | 
|  | 1549 | RHS = InsertNewInstBefore(new ExtractElementInst(RHS, 0U,"tmp"), *II); | 
|  | 1550 |  | 
|  | 1551 | switch (II->getIntrinsicID()) { | 
|  | 1552 | default: assert(0 && "Case stmts out of sync!"); | 
|  | 1553 | case Intrinsic::x86_sse_sub_ss: | 
|  | 1554 | case Intrinsic::x86_sse2_sub_sd: | 
|  | 1555 | TmpV = InsertNewInstBefore(BinaryOperator::createSub(LHS, RHS, | 
|  | 1556 | II->getName()), *II); | 
|  | 1557 | break; | 
|  | 1558 | case Intrinsic::x86_sse_mul_ss: | 
|  | 1559 | case Intrinsic::x86_sse2_mul_sd: | 
|  | 1560 | TmpV = InsertNewInstBefore(BinaryOperator::createMul(LHS, RHS, | 
|  | 1561 | II->getName()), *II); | 
|  | 1562 | break; | 
|  | 1563 | } | 
|  | 1564 |  | 
|  | 1565 | Instruction *New = | 
|  | 1566 | new InsertElementInst(UndefValue::get(II->getType()), TmpV, 0U, | 
|  | 1567 | II->getName()); | 
|  | 1568 | InsertNewInstBefore(New, *II); | 
|  | 1569 | AddSoonDeadInstToWorklist(*II, 0); | 
|  | 1570 | return New; | 
|  | 1571 | } | 
|  | 1572 | } | 
|  | 1573 |  | 
|  | 1574 | // Output elements are undefined if both are undefined.  Consider things | 
|  | 1575 | // like undef&0.  The result is known zero, not undef. | 
|  | 1576 | UndefElts &= UndefElts2; | 
|  | 1577 | break; | 
|  | 1578 | } | 
|  | 1579 | break; | 
|  | 1580 | } | 
|  | 1581 | } | 
|  | 1582 | return MadeChange ? I : 0; | 
|  | 1583 | } | 
|  | 1584 |  | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1585 | /// @returns true if the specified compare instruction is | 
|  | 1586 | /// true when both operands are equal... | 
|  | 1587 | /// @brief Determine if the ICmpInst returns true if both operands are equal | 
|  | 1588 | static bool isTrueWhenEqual(ICmpInst &ICI) { | 
|  | 1589 | ICmpInst::Predicate pred = ICI.getPredicate(); | 
|  | 1590 | return pred == ICmpInst::ICMP_EQ  || pred == ICmpInst::ICMP_UGE || | 
|  | 1591 | pred == ICmpInst::ICMP_SGE || pred == ICmpInst::ICMP_ULE || | 
|  | 1592 | pred == ICmpInst::ICMP_SLE; | 
|  | 1593 | } | 
|  | 1594 |  | 
| Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1595 | /// AssociativeOpt - Perform an optimization on an associative operator.  This | 
|  | 1596 | /// function is designed to check a chain of associative operators for a | 
|  | 1597 | /// potential to apply a certain optimization.  Since the optimization may be | 
|  | 1598 | /// applicable if the expression was reassociated, this checks the chain, then | 
|  | 1599 | /// reassociates the expression as necessary to expose the optimization | 
|  | 1600 | /// opportunity.  This makes use of a special Functor, which must define | 
|  | 1601 | /// 'shouldApply' and 'apply' methods. | 
|  | 1602 | /// | 
|  | 1603 | template<typename Functor> | 
|  | 1604 | Instruction *AssociativeOpt(BinaryOperator &Root, const Functor &F) { | 
|  | 1605 | unsigned Opcode = Root.getOpcode(); | 
|  | 1606 | Value *LHS = Root.getOperand(0); | 
|  | 1607 |  | 
|  | 1608 | // Quick check, see if the immediate LHS matches... | 
|  | 1609 | if (F.shouldApply(LHS)) | 
|  | 1610 | return F.apply(Root); | 
|  | 1611 |  | 
|  | 1612 | // Otherwise, if the LHS is not of the same opcode as the root, return. | 
|  | 1613 | Instruction *LHSI = dyn_cast<Instruction>(LHS); | 
| Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 1614 | while (LHSI && LHSI->getOpcode() == Opcode && LHSI->hasOneUse()) { | 
| Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1615 | // Should we apply this transform to the RHS? | 
|  | 1616 | bool ShouldApply = F.shouldApply(LHSI->getOperand(1)); | 
|  | 1617 |  | 
|  | 1618 | // If not to the RHS, check to see if we should apply to the LHS... | 
|  | 1619 | if (!ShouldApply && F.shouldApply(LHSI->getOperand(0))) { | 
|  | 1620 | cast<BinaryOperator>(LHSI)->swapOperands();   // Make the LHS the RHS | 
|  | 1621 | ShouldApply = true; | 
|  | 1622 | } | 
|  | 1623 |  | 
|  | 1624 | // If the functor wants to apply the optimization to the RHS of LHSI, | 
|  | 1625 | // reassociate the expression from ((? op A) op B) to (? op (A op B)) | 
|  | 1626 | if (ShouldApply) { | 
|  | 1627 | BasicBlock *BB = Root.getParent(); | 
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1628 |  | 
| Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1629 | // Now all of the instructions are in the current basic block, go ahead | 
|  | 1630 | // and perform the reassociation. | 
|  | 1631 | Instruction *TmpLHSI = cast<Instruction>(Root.getOperand(0)); | 
|  | 1632 |  | 
|  | 1633 | // First move the selected RHS to the LHS of the root... | 
|  | 1634 | Root.setOperand(0, LHSI->getOperand(1)); | 
|  | 1635 |  | 
|  | 1636 | // Make what used to be the LHS of the root be the user of the root... | 
|  | 1637 | Value *ExtraOperand = TmpLHSI->getOperand(1); | 
| Chris Lattner | 284d3b0 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 1638 | if (&Root == TmpLHSI) { | 
| Chris Lattner | 8953b90 | 2004-04-05 02:10:19 +0000 | [diff] [blame] | 1639 | Root.replaceAllUsesWith(Constant::getNullValue(TmpLHSI->getType())); | 
|  | 1640 | return 0; | 
|  | 1641 | } | 
| Chris Lattner | 284d3b0 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 1642 | Root.replaceAllUsesWith(TmpLHSI);          // Users now use TmpLHSI | 
| Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1643 | TmpLHSI->setOperand(1, &Root);             // TmpLHSI now uses the root | 
| Chris Lattner | 284d3b0 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 1644 | TmpLHSI->getParent()->getInstList().remove(TmpLHSI); | 
|  | 1645 | BasicBlock::iterator ARI = &Root; ++ARI; | 
|  | 1646 | BB->getInstList().insert(ARI, TmpLHSI);    // Move TmpLHSI to after Root | 
|  | 1647 | ARI = Root; | 
| Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1648 |  | 
|  | 1649 | // Now propagate the ExtraOperand down the chain of instructions until we | 
|  | 1650 | // get to LHSI. | 
|  | 1651 | while (TmpLHSI != LHSI) { | 
|  | 1652 | Instruction *NextLHSI = cast<Instruction>(TmpLHSI->getOperand(0)); | 
| Chris Lattner | 284d3b0 | 2004-04-16 18:08:07 +0000 | [diff] [blame] | 1653 | // Move the instruction to immediately before the chain we are | 
|  | 1654 | // constructing to avoid breaking dominance properties. | 
|  | 1655 | NextLHSI->getParent()->getInstList().remove(NextLHSI); | 
|  | 1656 | BB->getInstList().insert(ARI, NextLHSI); | 
|  | 1657 | ARI = NextLHSI; | 
|  | 1658 |  | 
| Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1659 | Value *NextOp = NextLHSI->getOperand(1); | 
|  | 1660 | NextLHSI->setOperand(1, ExtraOperand); | 
|  | 1661 | TmpLHSI = NextLHSI; | 
|  | 1662 | ExtraOperand = NextOp; | 
|  | 1663 | } | 
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1664 |  | 
| Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1665 | // Now that the instructions are reassociated, have the functor perform | 
|  | 1666 | // the transformation... | 
|  | 1667 | return F.apply(Root); | 
|  | 1668 | } | 
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1669 |  | 
| Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1670 | LHSI = dyn_cast<Instruction>(LHSI->getOperand(0)); | 
|  | 1671 | } | 
|  | 1672 | return 0; | 
|  | 1673 | } | 
|  | 1674 |  | 
|  | 1675 |  | 
|  | 1676 | // AddRHS - Implements: X + X --> X << 1 | 
|  | 1677 | struct AddRHS { | 
|  | 1678 | Value *RHS; | 
|  | 1679 | AddRHS(Value *rhs) : RHS(rhs) {} | 
|  | 1680 | bool shouldApply(Value *LHS) const { return LHS == RHS; } | 
|  | 1681 | Instruction *apply(BinaryOperator &Add) const { | 
| Reid Spencer | 0d5f923 | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 1682 | return BinaryOperator::createShl(Add.getOperand(0), | 
| Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1683 | ConstantInt::get(Add.getType(), 1)); | 
| Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1684 | } | 
|  | 1685 | }; | 
|  | 1686 |  | 
|  | 1687 | // AddMaskingAnd - Implements (A & C1)+(B & C2) --> (A & C1)|(B & C2) | 
|  | 1688 | //                 iff C1&C2 == 0 | 
|  | 1689 | struct AddMaskingAnd { | 
|  | 1690 | Constant *C2; | 
|  | 1691 | AddMaskingAnd(Constant *c) : C2(c) {} | 
|  | 1692 | bool shouldApply(Value *LHS) const { | 
| Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1693 | ConstantInt *C1; | 
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1694 | return match(LHS, m_And(m_Value(), m_ConstantInt(C1))) && | 
| Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1695 | ConstantExpr::getAnd(C1, C2)->isNullValue(); | 
| Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1696 | } | 
|  | 1697 | Instruction *apply(BinaryOperator &Add) const { | 
| Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1698 | return BinaryOperator::createOr(Add.getOperand(0), Add.getOperand(1)); | 
| Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1699 | } | 
|  | 1700 | }; | 
|  | 1701 |  | 
| Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1702 | static Value *FoldOperationIntoSelectOperand(Instruction &I, Value *SO, | 
| Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1703 | InstCombiner *IC) { | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1704 | if (CastInst *CI = dyn_cast<CastInst>(&I)) { | 
| Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1705 | if (Constant *SOC = dyn_cast<Constant>(SO)) | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1706 | return ConstantExpr::getCast(CI->getOpcode(), SOC, I.getType()); | 
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1707 |  | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1708 | return IC->InsertNewInstBefore(CastInst::create( | 
|  | 1709 | CI->getOpcode(), SO, I.getType(), SO->getName() + ".cast"), I); | 
| Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1710 | } | 
|  | 1711 |  | 
| Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1712 | // Figure out if the constant is the left or the right argument. | 
| Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1713 | bool ConstIsRHS = isa<Constant>(I.getOperand(1)); | 
|  | 1714 | Constant *ConstOperand = cast<Constant>(I.getOperand(ConstIsRHS)); | 
| Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1715 |  | 
| Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1716 | if (Constant *SOC = dyn_cast<Constant>(SO)) { | 
|  | 1717 | if (ConstIsRHS) | 
| Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1718 | return ConstantExpr::get(I.getOpcode(), SOC, ConstOperand); | 
|  | 1719 | return ConstantExpr::get(I.getOpcode(), ConstOperand, SOC); | 
| Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1720 | } | 
|  | 1721 |  | 
|  | 1722 | Value *Op0 = SO, *Op1 = ConstOperand; | 
|  | 1723 | if (!ConstIsRHS) | 
|  | 1724 | std::swap(Op0, Op1); | 
|  | 1725 | Instruction *New; | 
| Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1726 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I)) | 
|  | 1727 | New = BinaryOperator::create(BO->getOpcode(), Op0, Op1,SO->getName()+".op"); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1728 | else if (CmpInst *CI = dyn_cast<CmpInst>(&I)) | 
|  | 1729 | New = CmpInst::create(CI->getOpcode(), CI->getPredicate(), Op0, Op1, | 
|  | 1730 | SO->getName()+".cmp"); | 
| Chris Lattner | f9d9665 | 2004-04-10 19:15:56 +0000 | [diff] [blame] | 1731 | else { | 
| Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1732 | assert(0 && "Unknown binary instruction type!"); | 
| Chris Lattner | f9d9665 | 2004-04-10 19:15:56 +0000 | [diff] [blame] | 1733 | abort(); | 
|  | 1734 | } | 
| Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1735 | return IC->InsertNewInstBefore(New, I); | 
|  | 1736 | } | 
|  | 1737 |  | 
|  | 1738 | // FoldOpIntoSelect - Given an instruction with a select as one operand and a | 
|  | 1739 | // constant as the other operand, try to fold the binary operator into the | 
|  | 1740 | // select arguments.  This also works for Cast instructions, which obviously do | 
|  | 1741 | // not have a second operand. | 
|  | 1742 | static Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI, | 
|  | 1743 | InstCombiner *IC) { | 
|  | 1744 | // Don't modify shared select instructions | 
|  | 1745 | if (!SI->hasOneUse()) return 0; | 
|  | 1746 | Value *TV = SI->getOperand(1); | 
|  | 1747 | Value *FV = SI->getOperand(2); | 
|  | 1748 |  | 
|  | 1749 | if (isa<Constant>(TV) || isa<Constant>(FV)) { | 
| Chris Lattner | 374e659 | 2005-04-21 05:43:13 +0000 | [diff] [blame] | 1750 | // Bool selects with constant operands can be folded to logical ops. | 
| Reid Spencer | 542964f | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 1751 | if (SI->getType() == Type::Int1Ty) return 0; | 
| Chris Lattner | 374e659 | 2005-04-21 05:43:13 +0000 | [diff] [blame] | 1752 |  | 
| Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 1753 | Value *SelectTrueVal = FoldOperationIntoSelectOperand(Op, TV, IC); | 
|  | 1754 | Value *SelectFalseVal = FoldOperationIntoSelectOperand(Op, FV, IC); | 
|  | 1755 |  | 
|  | 1756 | return new SelectInst(SI->getCondition(), SelectTrueVal, | 
|  | 1757 | SelectFalseVal); | 
|  | 1758 | } | 
|  | 1759 | return 0; | 
| Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 1760 | } | 
|  | 1761 |  | 
| Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1762 |  | 
|  | 1763 | /// FoldOpIntoPhi - Given a binary operator or cast instruction which has a PHI | 
|  | 1764 | /// node as operand #0, see if we can fold the instruction into the PHI (which | 
|  | 1765 | /// is only possible if all operands to the PHI are constants). | 
|  | 1766 | Instruction *InstCombiner::FoldOpIntoPhi(Instruction &I) { | 
|  | 1767 | PHINode *PN = cast<PHINode>(I.getOperand(0)); | 
| Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 1768 | unsigned NumPHIValues = PN->getNumIncomingValues(); | 
| Chris Lattner | 0468987 | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1769 | if (!PN->hasOneUse() || NumPHIValues == 0) return 0; | 
| Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1770 |  | 
| Chris Lattner | 0468987 | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1771 | // Check to see if all of the operands of the PHI are constants.  If there is | 
|  | 1772 | // one non-constant value, remember the BB it is.  If there is more than one | 
| Chris Lattner | c4d8e7e | 2007-02-24 01:03:45 +0000 | [diff] [blame] | 1773 | // or if *it* is a PHI, bail out. | 
| Chris Lattner | 0468987 | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1774 | BasicBlock *NonConstBB = 0; | 
|  | 1775 | for (unsigned i = 0; i != NumPHIValues; ++i) | 
|  | 1776 | if (!isa<Constant>(PN->getIncomingValue(i))) { | 
|  | 1777 | if (NonConstBB) return 0;  // More than one non-const value. | 
| Chris Lattner | c4d8e7e | 2007-02-24 01:03:45 +0000 | [diff] [blame] | 1778 | if (isa<PHINode>(PN->getIncomingValue(i))) return 0;  // Itself a phi. | 
| Chris Lattner | 0468987 | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1779 | NonConstBB = PN->getIncomingBlock(i); | 
|  | 1780 |  | 
|  | 1781 | // If the incoming non-constant value is in I's block, we have an infinite | 
|  | 1782 | // loop. | 
|  | 1783 | if (NonConstBB == I.getParent()) | 
|  | 1784 | return 0; | 
|  | 1785 | } | 
|  | 1786 |  | 
|  | 1787 | // If there is exactly one non-constant value, we can insert a copy of the | 
|  | 1788 | // operation in that block.  However, if this is a critical edge, we would be | 
|  | 1789 | // inserting the computation one some other paths (e.g. inside a loop).  Only | 
|  | 1790 | // do this if the pred block is unconditionally branching into the phi block. | 
|  | 1791 | if (NonConstBB) { | 
|  | 1792 | BranchInst *BI = dyn_cast<BranchInst>(NonConstBB->getTerminator()); | 
|  | 1793 | if (!BI || !BI->isUnconditional()) return 0; | 
|  | 1794 | } | 
| Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1795 |  | 
|  | 1796 | // Okay, we can do the transformation: create the new PHI node. | 
| Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 1797 | PHINode *NewPN = new PHINode(I.getType(), ""); | 
| Chris Lattner | d8e2018 | 2005-01-29 00:39:08 +0000 | [diff] [blame] | 1798 | NewPN->reserveOperandSpace(PN->getNumOperands()/2); | 
| Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1799 | InsertNewInstBefore(NewPN, *PN); | 
| Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 1800 | NewPN->takeName(PN); | 
| Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1801 |  | 
|  | 1802 | // Next, add all of the operands to the PHI. | 
|  | 1803 | if (I.getNumOperands() == 2) { | 
|  | 1804 | Constant *C = cast<Constant>(I.getOperand(1)); | 
| Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 1805 | for (unsigned i = 0; i != NumPHIValues; ++i) { | 
| Chris Lattner | 0468987 | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1806 | Value *InV; | 
|  | 1807 | if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) { | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1808 | if (CmpInst *CI = dyn_cast<CmpInst>(&I)) | 
|  | 1809 | InV = ConstantExpr::getCompare(CI->getPredicate(), InC, C); | 
|  | 1810 | else | 
|  | 1811 | InV = ConstantExpr::get(I.getOpcode(), InC, C); | 
| Chris Lattner | 0468987 | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1812 | } else { | 
|  | 1813 | assert(PN->getIncomingBlock(i) == NonConstBB); | 
|  | 1814 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I)) | 
|  | 1815 | InV = BinaryOperator::create(BO->getOpcode(), | 
|  | 1816 | PN->getIncomingValue(i), C, "phitmp", | 
|  | 1817 | NonConstBB->getTerminator()); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1818 | else if (CmpInst *CI = dyn_cast<CmpInst>(&I)) | 
|  | 1819 | InV = CmpInst::create(CI->getOpcode(), | 
|  | 1820 | CI->getPredicate(), | 
|  | 1821 | PN->getIncomingValue(i), C, "phitmp", | 
|  | 1822 | NonConstBB->getTerminator()); | 
| Chris Lattner | 0468987 | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1823 | else | 
|  | 1824 | assert(0 && "Unknown binop!"); | 
|  | 1825 |  | 
| Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 1826 | AddToWorkList(cast<Instruction>(InV)); | 
| Chris Lattner | 0468987 | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1827 | } | 
|  | 1828 | NewPN->addIncoming(InV, PN->getIncomingBlock(i)); | 
| Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1829 | } | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1830 | } else { | 
|  | 1831 | CastInst *CI = cast<CastInst>(&I); | 
|  | 1832 | const Type *RetTy = CI->getType(); | 
| Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 1833 | for (unsigned i = 0; i != NumPHIValues; ++i) { | 
| Chris Lattner | 0468987 | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1834 | Value *InV; | 
|  | 1835 | if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) { | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1836 | InV = ConstantExpr::getCast(CI->getOpcode(), InC, RetTy); | 
| Chris Lattner | 0468987 | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1837 | } else { | 
|  | 1838 | assert(PN->getIncomingBlock(i) == NonConstBB); | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1839 | InV = CastInst::create(CI->getOpcode(), PN->getIncomingValue(i), | 
|  | 1840 | I.getType(), "phitmp", | 
|  | 1841 | NonConstBB->getTerminator()); | 
| Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 1842 | AddToWorkList(cast<Instruction>(InV)); | 
| Chris Lattner | 0468987 | 2006-09-09 22:02:56 +0000 | [diff] [blame] | 1843 | } | 
|  | 1844 | NewPN->addIncoming(InV, PN->getIncomingBlock(i)); | 
| Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1845 | } | 
|  | 1846 | } | 
|  | 1847 | return ReplaceInstUsesWith(I, NewPN); | 
|  | 1848 | } | 
|  | 1849 |  | 
| Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1850 | Instruction *InstCombiner::visitAdd(BinaryOperator &I) { | 
| Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 1851 | bool Changed = SimplifyCommutative(I); | 
| Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1852 | Value *LHS = I.getOperand(0), *RHS = I.getOperand(1); | 
| Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 1853 |  | 
| Chris Lattner | cf4a996 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 1854 | if (Constant *RHSC = dyn_cast<Constant>(RHS)) { | 
| Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 1855 | // X + undef -> undef | 
|  | 1856 | if (isa<UndefValue>(RHS)) | 
|  | 1857 | return ReplaceInstUsesWith(I, RHS); | 
|  | 1858 |  | 
| Chris Lattner | cf4a996 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 1859 | // X + 0 --> X | 
| Chris Lattner | 7a002fe | 2006-12-02 00:13:08 +0000 | [diff] [blame] | 1860 | if (!I.getType()->isFPOrFPVector()) { // NOTE: -0 + +0 = +0. | 
| Chris Lattner | 7fde91e | 2005-10-17 17:56:38 +0000 | [diff] [blame] | 1861 | if (RHSC->isNullValue()) | 
|  | 1862 | return ReplaceInstUsesWith(I, LHS); | 
| Chris Lattner | da1b152 | 2005-10-17 20:18:38 +0000 | [diff] [blame] | 1863 | } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) { | 
|  | 1864 | if (CFP->isExactlyValue(-0.0)) | 
|  | 1865 | return ReplaceInstUsesWith(I, LHS); | 
| Chris Lattner | 7fde91e | 2005-10-17 17:56:38 +0000 | [diff] [blame] | 1866 | } | 
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1867 |  | 
| Chris Lattner | cf4a996 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 1868 | if (ConstantInt *CI = dyn_cast<ConstantInt>(RHSC)) { | 
| Chris Lattner | 6e2c15c | 2006-11-09 05:12:27 +0000 | [diff] [blame] | 1869 | // X + (signbit) --> X ^ signbit | 
| Zhou Sheng | 150f3bb | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 1870 | const APInt& Val = CI->getValue(); | 
| Zhou Sheng | 56cda95 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 1871 | uint32_t BitWidth = Val.getBitWidth(); | 
| Reid Spencer | 959a21d | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 1872 | if (Val == APInt::getSignBit(BitWidth)) | 
| Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1873 | return BinaryOperator::createXor(LHS, RHS); | 
| Chris Lattner | 6e2c15c | 2006-11-09 05:12:27 +0000 | [diff] [blame] | 1874 |  | 
|  | 1875 | // See if SimplifyDemandedBits can simplify this.  This handles stuff like | 
|  | 1876 | // (X & 254)+1 -> (X&254)|1 | 
| Reid Spencer | 959a21d | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 1877 | if (!isa<VectorType>(I.getType())) { | 
|  | 1878 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); | 
|  | 1879 | if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth), | 
|  | 1880 | KnownZero, KnownOne)) | 
|  | 1881 | return &I; | 
|  | 1882 | } | 
| Chris Lattner | cf4a996 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 1883 | } | 
| Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 1884 |  | 
|  | 1885 | if (isa<PHINode>(LHS)) | 
|  | 1886 | if (Instruction *NV = FoldOpIntoPhi(I)) | 
|  | 1887 | return NV; | 
| Chris Lattner | 0b3557f | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 1888 |  | 
| Chris Lattner | 330628a | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 1889 | ConstantInt *XorRHS = 0; | 
|  | 1890 | Value *XorLHS = 0; | 
| Chris Lattner | 4284f64 | 2007-01-30 22:32:46 +0000 | [diff] [blame] | 1891 | if (isa<ConstantInt>(RHSC) && | 
|  | 1892 | match(LHS, m_Xor(m_Value(XorLHS), m_ConstantInt(XorRHS)))) { | 
| Zhou Sheng | 56cda95 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 1893 | uint32_t TySizeBits = I.getType()->getPrimitiveSizeInBits(); | 
| Zhou Sheng | 150f3bb | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 1894 | const APInt& RHSVal = cast<ConstantInt>(RHSC)->getValue(); | 
| Chris Lattner | 0b3557f | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 1895 |  | 
| Zhou Sheng | 56cda95 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 1896 | uint32_t Size = TySizeBits / 2; | 
| Reid Spencer | 959a21d | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 1897 | APInt C0080Val(APInt(TySizeBits, 1ULL).shl(Size - 1)); | 
|  | 1898 | APInt CFF80Val(-C0080Val); | 
| Chris Lattner | 0b3557f | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 1899 | do { | 
|  | 1900 | if (TySizeBits > Size) { | 
| Chris Lattner | 0b3557f | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 1901 | // If we have ADD(XOR(AND(X, 0xFF), 0x80), 0xF..F80), it's a sext. | 
|  | 1902 | // If we have ADD(XOR(AND(X, 0xFF), 0xF..F80), 0x80), it's a sext. | 
| Reid Spencer | 959a21d | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 1903 | if ((RHSVal == CFF80Val && XorRHS->getValue() == C0080Val) || | 
|  | 1904 | (RHSVal == C0080Val && XorRHS->getValue() == CFF80Val)) { | 
| Chris Lattner | 0b3557f | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 1905 | // This is a sign extend if the top bits are known zero. | 
| Zhou Sheng | b3a80b1 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 1906 | if (!MaskedValueIsZero(XorLHS, | 
|  | 1907 | APInt::getHighBitsSet(TySizeBits, TySizeBits - Size))) | 
| Chris Lattner | 0b3557f | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 1908 | Size = 0;  // Not a sign ext, but can't be any others either. | 
| Reid Spencer | 959a21d | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 1909 | break; | 
| Chris Lattner | 0b3557f | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 1910 | } | 
|  | 1911 | } | 
|  | 1912 | Size >>= 1; | 
| Reid Spencer | 959a21d | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 1913 | C0080Val = APIntOps::lshr(C0080Val, Size); | 
|  | 1914 | CFF80Val = APIntOps::ashr(CFF80Val, Size); | 
|  | 1915 | } while (Size >= 1); | 
| Chris Lattner | 0b3557f | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 1916 |  | 
| Reid Spencer | a5c18bf | 2007-03-28 01:36:16 +0000 | [diff] [blame] | 1917 | // FIXME: This shouldn't be necessary. When the backends can handle types | 
|  | 1918 | // with funny bit widths then this whole cascade of if statements should | 
|  | 1919 | // be removed. It is just here to get the size of the "middle" type back | 
|  | 1920 | // up to something that the back ends can handle. | 
|  | 1921 | const Type *MiddleType = 0; | 
|  | 1922 | switch (Size) { | 
|  | 1923 | default: break; | 
|  | 1924 | case 32: MiddleType = Type::Int32Ty; break; | 
|  | 1925 | case 16: MiddleType = Type::Int16Ty; break; | 
|  | 1926 | case  8: MiddleType = Type::Int8Ty; break; | 
|  | 1927 | } | 
|  | 1928 | if (MiddleType) { | 
| Reid Spencer | bb65ebf | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 1929 | Instruction *NewTrunc = new TruncInst(XorLHS, MiddleType, "sext"); | 
| Chris Lattner | 0b3557f | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 1930 | InsertNewInstBefore(NewTrunc, I); | 
| Reid Spencer | a5c18bf | 2007-03-28 01:36:16 +0000 | [diff] [blame] | 1931 | return new SExtInst(NewTrunc, I.getType(), I.getName()); | 
| Chris Lattner | 0b3557f | 2005-09-24 23:43:33 +0000 | [diff] [blame] | 1932 | } | 
|  | 1933 | } | 
| Chris Lattner | cf4a996 | 2004-04-10 22:01:55 +0000 | [diff] [blame] | 1934 | } | 
| Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 1935 |  | 
| Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1936 | // X + X --> X << 1 | 
| Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1937 | if (I.getType()->isInteger() && I.getType() != Type::Int1Ty) { | 
| Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1938 | if (Instruction *Result = AssociativeOpt(I, AddRHS(RHS))) return Result; | 
| Chris Lattner | 4706046 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 1939 |  | 
|  | 1940 | if (Instruction *RHSI = dyn_cast<Instruction>(RHS)) { | 
|  | 1941 | if (RHSI->getOpcode() == Instruction::Sub) | 
|  | 1942 | if (LHS == RHSI->getOperand(1))                   // A + (B - A) --> B | 
|  | 1943 | return ReplaceInstUsesWith(I, RHSI->getOperand(0)); | 
|  | 1944 | } | 
|  | 1945 | if (Instruction *LHSI = dyn_cast<Instruction>(LHS)) { | 
|  | 1946 | if (LHSI->getOpcode() == Instruction::Sub) | 
|  | 1947 | if (RHS == LHSI->getOperand(1))                   // (B - A) + A --> B | 
|  | 1948 | return ReplaceInstUsesWith(I, LHSI->getOperand(0)); | 
|  | 1949 | } | 
| Robert Bocchino | 7b5b86c | 2004-07-27 21:02:21 +0000 | [diff] [blame] | 1950 | } | 
| Chris Lattner | ede3fe0 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 1951 |  | 
| Chris Lattner | 147e975 | 2002-05-08 22:46:53 +0000 | [diff] [blame] | 1952 | // -A + B  -->  B - A | 
| Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 1953 | if (Value *V = dyn_castNegVal(LHS)) | 
| Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1954 | return BinaryOperator::createSub(RHS, V); | 
| Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 1955 |  | 
|  | 1956 | // A + -B  -->  A - B | 
| Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 1957 | if (!isa<Constant>(RHS)) | 
|  | 1958 | if (Value *V = dyn_castNegVal(RHS)) | 
| Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 1959 | return BinaryOperator::createSub(LHS, V); | 
| Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 1960 |  | 
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1961 |  | 
| Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 1962 | ConstantInt *C2; | 
|  | 1963 | if (Value *X = dyn_castFoldableMul(LHS, C2)) { | 
|  | 1964 | if (X == RHS)   // X*C + X --> X * (C+1) | 
|  | 1965 | return BinaryOperator::createMul(RHS, AddOne(C2)); | 
|  | 1966 |  | 
|  | 1967 | // X*C1 + X*C2 --> X * (C1+C2) | 
|  | 1968 | ConstantInt *C1; | 
|  | 1969 | if (X == dyn_castFoldableMul(RHS, C1)) | 
| Reid Spencer | 80263aa | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 1970 | return BinaryOperator::createMul(X, Add(C1, C2)); | 
| Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 1971 | } | 
|  | 1972 |  | 
|  | 1973 | // X + X*C --> X * (C+1) | 
| Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 1974 | if (dyn_castFoldableMul(RHS, C2) == LHS) | 
|  | 1975 | return BinaryOperator::createMul(LHS, AddOne(C2)); | 
|  | 1976 |  | 
| Chris Lattner | 23eb8ec | 2007-01-05 02:17:46 +0000 | [diff] [blame] | 1977 | // X + ~X --> -1   since   ~X = -X-1 | 
|  | 1978 | if (dyn_castNotVal(LHS) == RHS || | 
|  | 1979 | dyn_castNotVal(RHS) == LHS) | 
|  | 1980 | return ReplaceInstUsesWith(I, ConstantInt::getAllOnesValue(I.getType())); | 
|  | 1981 |  | 
| Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 1982 |  | 
| Chris Lattner | b8b9750 | 2003-08-13 19:01:45 +0000 | [diff] [blame] | 1983 | // (A & C1)+(B & C2) --> (A & C1)|(B & C2) iff C1&C2 == 0 | 
| Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1984 | if (match(RHS, m_And(m_Value(), m_ConstantInt(C2)))) | 
| Chris Lattner | 23eb8ec | 2007-01-05 02:17:46 +0000 | [diff] [blame] | 1985 | if (Instruction *R = AssociativeOpt(I, AddMaskingAnd(C2))) | 
|  | 1986 | return R; | 
| Chris Lattner | 7fb29e1 | 2003-03-11 00:12:48 +0000 | [diff] [blame] | 1987 |  | 
| Chris Lattner | b9cde76 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 1988 | if (ConstantInt *CRHS = dyn_cast<ConstantInt>(RHS)) { | 
| Chris Lattner | 330628a | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 1989 | Value *X = 0; | 
| Reid Spencer | 80263aa | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 1990 | if (match(LHS, m_Not(m_Value(X))))    // ~X + C --> (C-1) - X | 
|  | 1991 | return BinaryOperator::createSub(SubOne(CRHS), X); | 
| Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 1992 |  | 
| Chris Lattner | bff91d9 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 1993 | // (X & FF00) + xx00  -> (X+xx00) & FF00 | 
|  | 1994 | if (LHS->hasOneUse() && match(LHS, m_And(m_Value(X), m_ConstantInt(C2)))) { | 
| Reid Spencer | 80263aa | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 1995 | Constant *Anded = And(CRHS, C2); | 
| Chris Lattner | bff91d9 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 1996 | if (Anded == CRHS) { | 
|  | 1997 | // See if all bits from the first bit set in the Add RHS up are included | 
|  | 1998 | // in the mask.  First, get the rightmost bit. | 
| Zhou Sheng | 150f3bb | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 1999 | const APInt& AddRHSV = CRHS->getValue(); | 
| Chris Lattner | bff91d9 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2000 |  | 
|  | 2001 | // Form a mask of all bits from the lowest bit added through the top. | 
| Zhou Sheng | 150f3bb | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2002 | APInt AddRHSHighBits(~((AddRHSV & -AddRHSV)-1)); | 
| Chris Lattner | bff91d9 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2003 |  | 
|  | 2004 | // See if the and mask includes all of these bits. | 
| Zhou Sheng | 150f3bb | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2005 | APInt AddRHSHighBitsAnd(AddRHSHighBits & C2->getValue()); | 
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2006 |  | 
| Chris Lattner | bff91d9 | 2004-10-08 05:07:56 +0000 | [diff] [blame] | 2007 | if (AddRHSHighBits == AddRHSHighBitsAnd) { | 
|  | 2008 | // Okay, the xform is safe.  Insert the new add pronto. | 
|  | 2009 | Value *NewAdd = InsertNewInstBefore(BinaryOperator::createAdd(X, CRHS, | 
|  | 2010 | LHS->getName()), I); | 
|  | 2011 | return BinaryOperator::createAnd(NewAdd, C2); | 
|  | 2012 | } | 
|  | 2013 | } | 
|  | 2014 | } | 
|  | 2015 |  | 
| Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2016 | // Try to fold constant add into select arguments. | 
|  | 2017 | if (SelectInst *SI = dyn_cast<SelectInst>(LHS)) | 
| Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2018 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) | 
| Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2019 | return R; | 
| Chris Lattner | b9cde76 | 2003-10-02 15:11:26 +0000 | [diff] [blame] | 2020 | } | 
|  | 2021 |  | 
| Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2022 | // add (cast *A to intptrtype) B -> | 
|  | 2023 | //   cast (GEP (cast *A to sbyte*) B) -> | 
|  | 2024 | //     intptrtype | 
| Andrew Lenharth | 4f339be | 2006-09-19 18:24:51 +0000 | [diff] [blame] | 2025 | { | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2026 | CastInst *CI = dyn_cast<CastInst>(LHS); | 
|  | 2027 | Value *Other = RHS; | 
| Andrew Lenharth | 4f339be | 2006-09-19 18:24:51 +0000 | [diff] [blame] | 2028 | if (!CI) { | 
|  | 2029 | CI = dyn_cast<CastInst>(RHS); | 
|  | 2030 | Other = LHS; | 
|  | 2031 | } | 
| Andrew Lenharth | 44cb67a | 2006-09-20 15:37:57 +0000 | [diff] [blame] | 2032 | if (CI && CI->getType()->isSized() && | 
| Reid Spencer | 8f166b0 | 2007-01-08 16:32:00 +0000 | [diff] [blame] | 2033 | (CI->getType()->getPrimitiveSizeInBits() == | 
|  | 2034 | TD->getIntPtrType()->getPrimitiveSizeInBits()) | 
| Andrew Lenharth | 44cb67a | 2006-09-20 15:37:57 +0000 | [diff] [blame] | 2035 | && isa<PointerType>(CI->getOperand(0)->getType())) { | 
| Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 2036 | Value *I2 = InsertCastBefore(Instruction::BitCast, CI->getOperand(0), | 
| Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 2037 | PointerType::get(Type::Int8Ty), I); | 
| Andrew Lenharth | 44cb67a | 2006-09-20 15:37:57 +0000 | [diff] [blame] | 2038 | I2 = InsertNewInstBefore(new GetElementPtrInst(I2, Other, "ctg2"), I); | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2039 | return new PtrToIntInst(I2, CI->getType()); | 
| Andrew Lenharth | 4f339be | 2006-09-19 18:24:51 +0000 | [diff] [blame] | 2040 | } | 
|  | 2041 | } | 
|  | 2042 |  | 
| Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2043 | return Changed ? &I : 0; | 
| Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2044 | } | 
|  | 2045 |  | 
| Chris Lattner | bdb0ce0 | 2003-07-22 21:46:59 +0000 | [diff] [blame] | 2046 | // isSignBit - Return true if the value represented by the constant only has the | 
|  | 2047 | // highest order bit set. | 
|  | 2048 | static bool isSignBit(ConstantInt *CI) { | 
| Zhou Sheng | 56cda95 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 2049 | uint32_t NumBits = CI->getType()->getPrimitiveSizeInBits(); | 
| Reid Spencer | 450434e | 2007-03-19 20:58:18 +0000 | [diff] [blame] | 2050 | return CI->getValue() == APInt::getSignBit(NumBits); | 
| Chris Lattner | bdb0ce0 | 2003-07-22 21:46:59 +0000 | [diff] [blame] | 2051 | } | 
|  | 2052 |  | 
| Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2053 | Instruction *InstCombiner::visitSub(BinaryOperator &I) { | 
| Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2054 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); | 
| Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2055 |  | 
| Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 2056 | if (Op0 == Op1)         // sub X, X  -> 0 | 
|  | 2057 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); | 
| Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2058 |  | 
| Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 2059 | // If this is a 'B = x-(-A)', change to B = x+A... | 
| Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 2060 | if (Value *V = dyn_castNegVal(Op1)) | 
| Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2061 | return BinaryOperator::createAdd(Op0, V); | 
| Chris Lattner | 9fa53de | 2002-05-06 16:49:18 +0000 | [diff] [blame] | 2062 |  | 
| Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2063 | if (isa<UndefValue>(Op0)) | 
|  | 2064 | return ReplaceInstUsesWith(I, Op0);    // undef - X -> undef | 
|  | 2065 | if (isa<UndefValue>(Op1)) | 
|  | 2066 | return ReplaceInstUsesWith(I, Op1);    // X - undef -> undef | 
|  | 2067 |  | 
| Chris Lattner | 8f2f598 | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 2068 | if (ConstantInt *C = dyn_cast<ConstantInt>(Op0)) { | 
|  | 2069 | // Replace (-1 - A) with (~A)... | 
| Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2070 | if (C->isAllOnesValue()) | 
|  | 2071 | return BinaryOperator::createNot(Op1); | 
| Chris Lattner | ad3c495 | 2002-05-09 01:29:19 +0000 | [diff] [blame] | 2072 |  | 
| Chris Lattner | 8f2f598 | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 2073 | // C - ~X == X + (1+C) | 
| Reid Spencer | 4fdd96c | 2005-06-18 17:37:34 +0000 | [diff] [blame] | 2074 | Value *X = 0; | 
| Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 2075 | if (match(Op1, m_Not(m_Value(X)))) | 
| Reid Spencer | 80263aa | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2076 | return BinaryOperator::createAdd(X, AddOne(C)); | 
|  | 2077 |  | 
| Chris Lattner | 27df1db | 2007-01-15 07:02:54 +0000 | [diff] [blame] | 2078 | // -(X >>u 31) -> (X >>s 31) | 
|  | 2079 | // -(X >>s 31) -> (X >>u 31) | 
| Zhou Sheng | fd28a33 | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 2080 | if (C->isZero()) { | 
| Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2081 | if (BinaryOperator *SI = dyn_cast<BinaryOperator>(Op1)) | 
| Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2082 | if (SI->getOpcode() == Instruction::LShr) { | 
| Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2083 | if (ConstantInt *CU = dyn_cast<ConstantInt>(SI->getOperand(1))) { | 
| Chris Lattner | 92295c5 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 2084 | // Check to see if we are shifting out everything but the sign bit. | 
| Zhou Sheng | fd28a33 | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 2085 | if (CU->getLimitedValue(SI->getType()->getPrimitiveSizeInBits()) == | 
| Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2086 | SI->getType()->getPrimitiveSizeInBits()-1) { | 
| Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2087 | // Ok, the transformation is safe.  Insert AShr. | 
| Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2088 | return BinaryOperator::create(Instruction::AShr, | 
|  | 2089 | SI->getOperand(0), CU, SI->getName()); | 
| Chris Lattner | 92295c5 | 2004-03-12 23:53:13 +0000 | [diff] [blame] | 2090 | } | 
|  | 2091 | } | 
| Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2092 | } | 
|  | 2093 | else if (SI->getOpcode() == Instruction::AShr) { | 
|  | 2094 | if (ConstantInt *CU = dyn_cast<ConstantInt>(SI->getOperand(1))) { | 
|  | 2095 | // Check to see if we are shifting out everything but the sign bit. | 
| Zhou Sheng | fd28a33 | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 2096 | if (CU->getLimitedValue(SI->getType()->getPrimitiveSizeInBits()) == | 
| Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2097 | SI->getType()->getPrimitiveSizeInBits()-1) { | 
| Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 2098 | // Ok, the transformation is safe.  Insert LShr. | 
| Reid Spencer | 0d5f923 | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 2099 | return BinaryOperator::createLShr( | 
| Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2100 | SI->getOperand(0), CU, SI->getName()); | 
| Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2101 | } | 
|  | 2102 | } | 
|  | 2103 | } | 
| Chris Lattner | 022167f | 2004-03-13 00:11:49 +0000 | [diff] [blame] | 2104 | } | 
| Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2105 |  | 
|  | 2106 | // Try to fold constant sub into select arguments. | 
|  | 2107 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) | 
| Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2108 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) | 
| Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2109 | return R; | 
| Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2110 |  | 
|  | 2111 | if (isa<PHINode>(Op0)) | 
|  | 2112 | if (Instruction *NV = FoldOpIntoPhi(I)) | 
|  | 2113 | return NV; | 
| Chris Lattner | 8f2f598 | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 2114 | } | 
|  | 2115 |  | 
| Chris Lattner | a9be449 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2116 | if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) { | 
|  | 2117 | if (Op1I->getOpcode() == Instruction::Add && | 
| Chris Lattner | 7a002fe | 2006-12-02 00:13:08 +0000 | [diff] [blame] | 2118 | !Op0->getType()->isFPOrFPVector()) { | 
| Chris Lattner | c7f3c1a | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 2119 | if (Op1I->getOperand(0) == Op0)              // X-(X+Y) == -Y | 
| Chris Lattner | a9be449 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2120 | return BinaryOperator::createNeg(Op1I->getOperand(1), I.getName()); | 
| Chris Lattner | c7f3c1a | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 2121 | else if (Op1I->getOperand(1) == Op0)         // X-(Y+X) == -Y | 
| Chris Lattner | a9be449 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2122 | return BinaryOperator::createNeg(Op1I->getOperand(0), I.getName()); | 
| Chris Lattner | c7f3c1a | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 2123 | else if (ConstantInt *CI1 = dyn_cast<ConstantInt>(I.getOperand(0))) { | 
|  | 2124 | if (ConstantInt *CI2 = dyn_cast<ConstantInt>(Op1I->getOperand(1))) | 
|  | 2125 | // C1-(X+C2) --> (C1-C2)-X | 
| Reid Spencer | 80263aa | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2126 | return BinaryOperator::createSub(Subtract(CI1, CI2), | 
| Chris Lattner | c7f3c1a | 2005-04-07 16:28:01 +0000 | [diff] [blame] | 2127 | Op1I->getOperand(0)); | 
|  | 2128 | } | 
| Chris Lattner | a9be449 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2129 | } | 
|  | 2130 |  | 
| Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 2131 | if (Op1I->hasOneUse()) { | 
| Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2132 | // Replace (x - (y - z)) with (x + (z - y)) if the (y - z) subexpression | 
|  | 2133 | // is not used by anyone else... | 
|  | 2134 | // | 
| Chris Lattner | c2f0aa5 | 2004-02-02 20:09:56 +0000 | [diff] [blame] | 2135 | if (Op1I->getOpcode() == Instruction::Sub && | 
| Chris Lattner | 7a002fe | 2006-12-02 00:13:08 +0000 | [diff] [blame] | 2136 | !Op1I->getType()->isFPOrFPVector()) { | 
| Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2137 | // Swap the two operands of the subexpr... | 
|  | 2138 | Value *IIOp0 = Op1I->getOperand(0), *IIOp1 = Op1I->getOperand(1); | 
|  | 2139 | Op1I->setOperand(0, IIOp1); | 
|  | 2140 | Op1I->setOperand(1, IIOp0); | 
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2141 |  | 
| Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2142 | // Create the new top level add instruction... | 
| Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2143 | return BinaryOperator::createAdd(Op0, Op1); | 
| Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2144 | } | 
|  | 2145 |  | 
|  | 2146 | // Replace (A - (A & B)) with (A & ~B) if this is the only use of (A&B)... | 
|  | 2147 | // | 
|  | 2148 | if (Op1I->getOpcode() == Instruction::And && | 
|  | 2149 | (Op1I->getOperand(0) == Op0 || Op1I->getOperand(1) == Op0)) { | 
|  | 2150 | Value *OtherOp = Op1I->getOperand(Op1I->getOperand(0) == Op0); | 
|  | 2151 |  | 
| Chris Lattner | 396dbfe | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 2152 | Value *NewNot = | 
|  | 2153 | InsertNewInstBefore(BinaryOperator::createNot(OtherOp, "B.not"), I); | 
| Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2154 | return BinaryOperator::createAnd(Op0, NewNot); | 
| Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2155 | } | 
| Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2156 |  | 
| Reid Spencer | 3c51495 | 2006-10-16 23:08:08 +0000 | [diff] [blame] | 2157 | // 0 - (X sdiv C)  -> (X sdiv -C) | 
| Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2158 | if (Op1I->getOpcode() == Instruction::SDiv) | 
| Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2159 | if (ConstantInt *CSI = dyn_cast<ConstantInt>(Op0)) | 
| Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2160 | if (CSI->isNullValue()) | 
| Chris Lattner | 0aee4b7 | 2004-10-06 15:08:25 +0000 | [diff] [blame] | 2161 | if (Constant *DivRHS = dyn_cast<Constant>(Op1I->getOperand(1))) | 
| Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2162 | return BinaryOperator::createSDiv(Op1I->getOperand(0), | 
| Chris Lattner | 0aee4b7 | 2004-10-06 15:08:25 +0000 | [diff] [blame] | 2163 | ConstantExpr::getNeg(DivRHS)); | 
|  | 2164 |  | 
| Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2165 | // X - X*C --> X * (1-C) | 
| Reid Spencer | 4fdd96c | 2005-06-18 17:37:34 +0000 | [diff] [blame] | 2166 | ConstantInt *C2 = 0; | 
| Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2167 | if (dyn_castFoldableMul(Op1I, C2) == Op0) { | 
| Reid Spencer | 80263aa | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2168 | Constant *CP1 = Subtract(ConstantInt::get(I.getType(), 1), C2); | 
| Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2169 | return BinaryOperator::createMul(Op0, CP1); | 
| Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2170 | } | 
| Chris Lattner | ad3c495 | 2002-05-09 01:29:19 +0000 | [diff] [blame] | 2171 | } | 
| Chris Lattner | a9be449 | 2005-04-07 16:15:25 +0000 | [diff] [blame] | 2172 | } | 
| Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2173 |  | 
| Chris Lattner | 7a002fe | 2006-12-02 00:13:08 +0000 | [diff] [blame] | 2174 | if (!Op0->getType()->isFPOrFPVector()) | 
| Chris Lattner | 4706046 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 2175 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) | 
|  | 2176 | if (Op0I->getOpcode() == Instruction::Add) { | 
| Chris Lattner | 411336f | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 2177 | if (Op0I->getOperand(0) == Op1)             // (Y+X)-Y == X | 
|  | 2178 | return ReplaceInstUsesWith(I, Op0I->getOperand(1)); | 
|  | 2179 | else if (Op0I->getOperand(1) == Op1)        // (X+Y)-Y == X | 
|  | 2180 | return ReplaceInstUsesWith(I, Op0I->getOperand(0)); | 
| Chris Lattner | 4706046 | 2005-04-07 17:14:51 +0000 | [diff] [blame] | 2181 | } else if (Op0I->getOpcode() == Instruction::Sub) { | 
|  | 2182 | if (Op0I->getOperand(0) == Op1)             // (X-Y)-X == -Y | 
|  | 2183 | return BinaryOperator::createNeg(Op0I->getOperand(1), I.getName()); | 
| Chris Lattner | 411336f | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 2184 | } | 
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2185 |  | 
| Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2186 | ConstantInt *C1; | 
|  | 2187 | if (Value *X = dyn_castFoldableMul(Op0, C1)) { | 
| Reid Spencer | 80263aa | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2188 | if (X == Op1)  // X*C - X --> X * (C-1) | 
|  | 2189 | return BinaryOperator::createMul(Op1, SubOne(C1)); | 
| Chris Lattner | 57c8d99 | 2003-02-18 19:57:07 +0000 | [diff] [blame] | 2190 |  | 
| Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2191 | ConstantInt *C2;   // X*C1 - X*C2 -> X * (C1-C2) | 
|  | 2192 | if (X == dyn_castFoldableMul(Op1, C2)) | 
| Reid Spencer | 80263aa | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2193 | return BinaryOperator::createMul(Op1, Subtract(C1, C2)); | 
| Chris Lattner | 8c3e7b9 | 2004-11-13 19:50:12 +0000 | [diff] [blame] | 2194 | } | 
| Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2195 | return 0; | 
| Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2196 | } | 
|  | 2197 |  | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2198 | /// isSignBitCheck - Given an exploded icmp instruction, return true if it | 
| Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2199 | /// really just returns true if the most significant (sign) bit is set. | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2200 | static bool isSignBitCheck(ICmpInst::Predicate pred, ConstantInt *RHS) { | 
|  | 2201 | switch (pred) { | 
|  | 2202 | case ICmpInst::ICMP_SLT: | 
|  | 2203 | // True if LHS s< RHS and RHS == 0 | 
|  | 2204 | return RHS->isNullValue(); | 
|  | 2205 | case ICmpInst::ICMP_SLE: | 
|  | 2206 | // True if LHS s<= RHS and RHS == -1 | 
|  | 2207 | return RHS->isAllOnesValue(); | 
|  | 2208 | case ICmpInst::ICMP_UGE: | 
|  | 2209 | // True if LHS u>= RHS and RHS == high-bit-mask (2^7, 2^15, 2^31, etc) | 
| Reid Spencer | a962d18 | 2007-03-24 00:42:08 +0000 | [diff] [blame] | 2210 | return RHS->getValue() == | 
|  | 2211 | APInt::getSignBit(RHS->getType()->getPrimitiveSizeInBits()); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2212 | case ICmpInst::ICMP_UGT: | 
|  | 2213 | // True if LHS u> RHS and RHS == high-bit-mask - 1 | 
| Reid Spencer | a962d18 | 2007-03-24 00:42:08 +0000 | [diff] [blame] | 2214 | return RHS->getValue() == | 
|  | 2215 | APInt::getSignedMaxValue(RHS->getType()->getPrimitiveSizeInBits()); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2216 | default: | 
|  | 2217 | return false; | 
| Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2218 | } | 
| Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2219 | } | 
|  | 2220 |  | 
| Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2221 | Instruction *InstCombiner::visitMul(BinaryOperator &I) { | 
| Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 2222 | bool Changed = SimplifyCommutative(I); | 
| Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2223 | Value *Op0 = I.getOperand(0); | 
| Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2224 |  | 
| Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2225 | if (isa<UndefValue>(I.getOperand(1)))              // undef * X -> 0 | 
|  | 2226 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); | 
|  | 2227 |  | 
| Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 2228 | // Simplify mul instructions with a constant RHS... | 
| Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2229 | if (Constant *Op1 = dyn_cast<Constant>(I.getOperand(1))) { | 
|  | 2230 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { | 
| Chris Lattner | ede3fe0 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 2231 |  | 
|  | 2232 | // ((X << C1)*C2) == (X * (C2 << C1)) | 
| Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2233 | if (BinaryOperator *SI = dyn_cast<BinaryOperator>(Op0)) | 
| Chris Lattner | ede3fe0 | 2003-08-13 04:18:28 +0000 | [diff] [blame] | 2234 | if (SI->getOpcode() == Instruction::Shl) | 
|  | 2235 | if (Constant *ShOp = dyn_cast<Constant>(SI->getOperand(1))) | 
| Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2236 | return BinaryOperator::createMul(SI->getOperand(0), | 
|  | 2237 | ConstantExpr::getShl(CI, ShOp)); | 
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2238 |  | 
| Chris Lattner | cce81be | 2003-09-11 22:24:54 +0000 | [diff] [blame] | 2239 | if (CI->isNullValue()) | 
|  | 2240 | return ReplaceInstUsesWith(I, Op1);  // X * 0  == 0 | 
|  | 2241 | if (CI->equalsInt(1))                  // X * 1  == X | 
|  | 2242 | return ReplaceInstUsesWith(I, Op0); | 
|  | 2243 | if (CI->isAllOnesValue())              // X * -1 == 0 - X | 
| Chris Lattner | 35236d8 | 2003-06-25 17:09:20 +0000 | [diff] [blame] | 2244 | return BinaryOperator::createNeg(Op0, I.getName()); | 
| Chris Lattner | 31ba129 | 2002-04-29 22:24:47 +0000 | [diff] [blame] | 2245 |  | 
| Zhou Sheng | 4961cf1 | 2007-03-29 01:57:21 +0000 | [diff] [blame] | 2246 | const APInt& Val = cast<ConstantInt>(CI)->getValue(); | 
| Reid Spencer | 6d39206 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2247 | if (Val.isPowerOf2()) {          // Replace X*(2^C) with X << C | 
| Reid Spencer | 0d5f923 | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 2248 | return BinaryOperator::createShl(Op0, | 
| Reid Spencer | 6d39206 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2249 | ConstantInt::get(Op0->getType(), Val.logBase2())); | 
| Chris Lattner | 22d00a8 | 2005-08-02 19:16:58 +0000 | [diff] [blame] | 2250 | } | 
| Robert Bocchino | 7b5b86c | 2004-07-27 21:02:21 +0000 | [diff] [blame] | 2251 | } else if (ConstantFP *Op1F = dyn_cast<ConstantFP>(Op1)) { | 
| Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2252 | if (Op1F->isNullValue()) | 
|  | 2253 | return ReplaceInstUsesWith(I, Op1); | 
| Chris Lattner | 31ba129 | 2002-04-29 22:24:47 +0000 | [diff] [blame] | 2254 |  | 
| Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2255 | // "In IEEE floating point, x*1 is not equivalent to x for nans.  However, | 
|  | 2256 | // ANSI says we can drop signals, so we can do this anyway." (from GCC) | 
|  | 2257 | if (Op1F->getValue() == 1.0) | 
|  | 2258 | return ReplaceInstUsesWith(I, Op0);  // Eliminate 'mul double %X, 1.0' | 
|  | 2259 | } | 
| Chris Lattner | 32c01df | 2006-03-04 06:04:02 +0000 | [diff] [blame] | 2260 |  | 
|  | 2261 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) | 
|  | 2262 | if (Op0I->getOpcode() == Instruction::Add && Op0I->hasOneUse() && | 
|  | 2263 | isa<ConstantInt>(Op0I->getOperand(1))) { | 
|  | 2264 | // Canonicalize (X+C1)*C2 -> X*C2+C1*C2. | 
|  | 2265 | Instruction *Add = BinaryOperator::createMul(Op0I->getOperand(0), | 
|  | 2266 | Op1, "tmp"); | 
|  | 2267 | InsertNewInstBefore(Add, I); | 
|  | 2268 | Value *C1C2 = ConstantExpr::getMul(Op1, | 
|  | 2269 | cast<Constant>(Op0I->getOperand(1))); | 
|  | 2270 | return BinaryOperator::createAdd(Add, C1C2); | 
|  | 2271 |  | 
|  | 2272 | } | 
| Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2273 |  | 
|  | 2274 | // Try to fold constant mul into select arguments. | 
|  | 2275 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) | 
| Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2276 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) | 
| Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 2277 | return R; | 
| Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 2278 |  | 
|  | 2279 | if (isa<PHINode>(Op0)) | 
|  | 2280 | if (Instruction *NV = FoldOpIntoPhi(I)) | 
|  | 2281 | return NV; | 
| Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2282 | } | 
|  | 2283 |  | 
| Chris Lattner | 934a64cf | 2003-03-10 23:23:04 +0000 | [diff] [blame] | 2284 | if (Value *Op0v = dyn_castNegVal(Op0))     // -X * -Y = X*Y | 
|  | 2285 | if (Value *Op1v = dyn_castNegVal(I.getOperand(1))) | 
| Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2286 | return BinaryOperator::createMul(Op0v, Op1v); | 
| Chris Lattner | 934a64cf | 2003-03-10 23:23:04 +0000 | [diff] [blame] | 2287 |  | 
| Chris Lattner | 2635b52 | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2288 | // If one of the operands of the multiply is a cast from a boolean value, then | 
|  | 2289 | // we know the bool is either zero or one, so this is a 'masking' multiply. | 
|  | 2290 | // See if we can simplify things based on how the boolean was originally | 
|  | 2291 | // formed. | 
|  | 2292 | CastInst *BoolCast = 0; | 
| Reid Spencer | 74a528b | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 2293 | if (ZExtInst *CI = dyn_cast<ZExtInst>(I.getOperand(0))) | 
| Reid Spencer | 542964f | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 2294 | if (CI->getOperand(0)->getType() == Type::Int1Ty) | 
| Chris Lattner | 2635b52 | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2295 | BoolCast = CI; | 
|  | 2296 | if (!BoolCast) | 
| Reid Spencer | 74a528b | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 2297 | if (ZExtInst *CI = dyn_cast<ZExtInst>(I.getOperand(1))) | 
| Reid Spencer | 542964f | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 2298 | if (CI->getOperand(0)->getType() == Type::Int1Ty) | 
| Chris Lattner | 2635b52 | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2299 | BoolCast = CI; | 
|  | 2300 | if (BoolCast) { | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2301 | if (ICmpInst *SCI = dyn_cast<ICmpInst>(BoolCast->getOperand(0))) { | 
| Chris Lattner | 2635b52 | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2302 | Value *SCIOp0 = SCI->getOperand(0), *SCIOp1 = SCI->getOperand(1); | 
|  | 2303 | const Type *SCOpTy = SCIOp0->getType(); | 
|  | 2304 |  | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2305 | // If the icmp is true iff the sign bit of X is set, then convert this | 
| Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2306 | // multiply into a shift/and combination. | 
|  | 2307 | if (isa<ConstantInt>(SCIOp1) && | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2308 | isSignBitCheck(SCI->getPredicate(), cast<ConstantInt>(SCIOp1))) { | 
| Chris Lattner | 2635b52 | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2309 | // Shift the X value right to turn it into "all signbits". | 
| Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2310 | Constant *Amt = ConstantInt::get(SCIOp0->getType(), | 
| Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 2311 | SCOpTy->getPrimitiveSizeInBits()-1); | 
| Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2312 | Value *V = | 
| Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2313 | InsertNewInstBefore( | 
|  | 2314 | BinaryOperator::create(Instruction::AShr, SCIOp0, Amt, | 
| Chris Lattner | e79e854 | 2004-02-23 06:38:22 +0000 | [diff] [blame] | 2315 | BoolCast->getOperand(0)->getName()+ | 
|  | 2316 | ".mask"), I); | 
| Chris Lattner | 2635b52 | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2317 |  | 
|  | 2318 | // If the multiply type is not the same as the source type, sign extend | 
|  | 2319 | // or truncate to the multiply type. | 
| Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 2320 | if (I.getType() != V->getType()) { | 
| Zhou Sheng | 56cda95 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 2321 | uint32_t SrcBits = V->getType()->getPrimitiveSizeInBits(); | 
|  | 2322 | uint32_t DstBits = I.getType()->getPrimitiveSizeInBits(); | 
| Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 2323 | Instruction::CastOps opcode = | 
|  | 2324 | (SrcBits == DstBits ? Instruction::BitCast : | 
|  | 2325 | (SrcBits < DstBits ? Instruction::SExt : Instruction::Trunc)); | 
|  | 2326 | V = InsertCastBefore(opcode, V, I.getType(), I); | 
|  | 2327 | } | 
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2328 |  | 
| Chris Lattner | 2635b52 | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2329 | Value *OtherOp = Op0 == BoolCast ? I.getOperand(1) : Op0; | 
| Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2330 | return BinaryOperator::createAnd(V, OtherOp); | 
| Chris Lattner | 2635b52 | 2004-02-23 05:39:21 +0000 | [diff] [blame] | 2331 | } | 
|  | 2332 | } | 
|  | 2333 | } | 
|  | 2334 |  | 
| Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2335 | return Changed ? &I : 0; | 
| Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 2336 | } | 
|  | 2337 |  | 
| Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2338 | /// This function implements the transforms on div instructions that work | 
|  | 2339 | /// regardless of the kind of div instruction it is (udiv, sdiv, or fdiv). It is | 
|  | 2340 | /// used by the visitors to those instructions. | 
|  | 2341 | /// @brief Transforms common to all three div instructions | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2342 | Instruction *InstCombiner::commonDivTransforms(BinaryOperator &I) { | 
| Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2343 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); | 
| Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 2344 |  | 
| Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2345 | // undef / X -> 0 | 
|  | 2346 | if (isa<UndefValue>(Op0)) | 
| Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2347 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); | 
| Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2348 |  | 
|  | 2349 | // X / undef -> undef | 
| Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2350 | if (isa<UndefValue>(Op1)) | 
| Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2351 | return ReplaceInstUsesWith(I, Op1); | 
| Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2352 |  | 
| Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2353 | // Handle cases involving: div X, (select Cond, Y, Z) | 
| Chris Lattner | d79dc79 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2354 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) { | 
|  | 2355 | // div X, (Cond ? 0 : Y) -> div X, Y.  If the div and the select are in the | 
| Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2356 | // same basic block, then we replace the select with Y, and the condition | 
|  | 2357 | // of the select with false (if the cond value is in the same BB).  If the | 
| Chris Lattner | d79dc79 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2358 | // select has uses other than the div, this allows them to be simplified | 
| Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2359 | // also. Note that div X, Y is just as good as div X, 0 (undef) | 
| Chris Lattner | d79dc79 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2360 | if (Constant *ST = dyn_cast<Constant>(SI->getOperand(1))) | 
|  | 2361 | if (ST->isNullValue()) { | 
|  | 2362 | Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0)); | 
|  | 2363 | if (CondI && CondI->getParent() == I.getParent()) | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2364 | UpdateValueUsesWith(CondI, ConstantInt::getFalse()); | 
| Chris Lattner | d79dc79 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2365 | else if (I.getParent() != SI->getParent() || SI->hasOneUse()) | 
|  | 2366 | I.setOperand(1, SI->getOperand(2)); | 
|  | 2367 | else | 
|  | 2368 | UpdateValueUsesWith(SI, SI->getOperand(2)); | 
|  | 2369 | return &I; | 
|  | 2370 | } | 
| Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2371 |  | 
| Chris Lattner | d79dc79 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2372 | // Likewise for: div X, (Cond ? Y : 0) -> div X, Y | 
|  | 2373 | if (Constant *ST = dyn_cast<Constant>(SI->getOperand(2))) | 
|  | 2374 | if (ST->isNullValue()) { | 
|  | 2375 | Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0)); | 
|  | 2376 | if (CondI && CondI->getParent() == I.getParent()) | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2377 | UpdateValueUsesWith(CondI, ConstantInt::getTrue()); | 
| Chris Lattner | d79dc79 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2378 | else if (I.getParent() != SI->getParent() || SI->hasOneUse()) | 
|  | 2379 | I.setOperand(1, SI->getOperand(1)); | 
|  | 2380 | else | 
|  | 2381 | UpdateValueUsesWith(SI, SI->getOperand(1)); | 
|  | 2382 | return &I; | 
|  | 2383 | } | 
| Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2384 | } | 
| Chris Lattner | d79dc79 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2385 |  | 
| Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2386 | return 0; | 
|  | 2387 | } | 
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2388 |  | 
| Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2389 | /// This function implements the transforms common to both integer division | 
|  | 2390 | /// instructions (udiv and sdiv). It is called by the visitors to those integer | 
|  | 2391 | /// division instructions. | 
|  | 2392 | /// @brief Common integer divide transforms | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2393 | Instruction *InstCombiner::commonIDivTransforms(BinaryOperator &I) { | 
| Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2394 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); | 
|  | 2395 |  | 
|  | 2396 | if (Instruction *Common = commonDivTransforms(I)) | 
|  | 2397 | return Common; | 
|  | 2398 |  | 
|  | 2399 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { | 
|  | 2400 | // div X, 1 == X | 
|  | 2401 | if (RHS->equalsInt(1)) | 
|  | 2402 | return ReplaceInstUsesWith(I, Op0); | 
|  | 2403 |  | 
|  | 2404 | // (X / C1) / C2  -> X / (C1*C2) | 
|  | 2405 | if (Instruction *LHS = dyn_cast<Instruction>(Op0)) | 
|  | 2406 | if (Instruction::BinaryOps(LHS->getOpcode()) == I.getOpcode()) | 
|  | 2407 | if (ConstantInt *LHSRHS = dyn_cast<ConstantInt>(LHS->getOperand(1))) { | 
|  | 2408 | return BinaryOperator::create(I.getOpcode(), LHS->getOperand(0), | 
| Reid Spencer | 80263aa | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2409 | Multiply(RHS, LHSRHS)); | 
| Chris Lattner | 4236261 | 2005-04-08 04:03:26 +0000 | [diff] [blame] | 2410 | } | 
| Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2411 |  | 
| Reid Spencer | 6d39206 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2412 | if (!RHS->isZero()) { // avoid X udiv 0 | 
| Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2413 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) | 
|  | 2414 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) | 
|  | 2415 | return R; | 
|  | 2416 | if (isa<PHINode>(Op0)) | 
|  | 2417 | if (Instruction *NV = FoldOpIntoPhi(I)) | 
|  | 2418 | return NV; | 
|  | 2419 | } | 
| Chris Lattner | d79dc79 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2420 | } | 
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2421 |  | 
| Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2422 | // 0 / X == 0, we don't need to preserve faults! | 
| Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2423 | if (ConstantInt *LHS = dyn_cast<ConstantInt>(Op0)) | 
| Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2424 | if (LHS->equalsInt(0)) | 
|  | 2425 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); | 
|  | 2426 |  | 
| Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2427 | return 0; | 
|  | 2428 | } | 
|  | 2429 |  | 
|  | 2430 | Instruction *InstCombiner::visitUDiv(BinaryOperator &I) { | 
|  | 2431 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); | 
|  | 2432 |  | 
|  | 2433 | // Handle the integer div common cases | 
|  | 2434 | if (Instruction *Common = commonIDivTransforms(I)) | 
|  | 2435 | return Common; | 
|  | 2436 |  | 
|  | 2437 | // X udiv C^2 -> X >> C | 
|  | 2438 | // Check to see if this is an unsigned division with an exact power of 2, | 
|  | 2439 | // if so, convert to a right shift. | 
|  | 2440 | if (ConstantInt *C = dyn_cast<ConstantInt>(Op1)) { | 
| Reid Spencer | 54d5b1b | 2007-03-26 23:58:26 +0000 | [diff] [blame] | 2441 | if (C->getValue().isPowerOf2())  // 0 not included in isPowerOf2 | 
| Reid Spencer | 6d39206 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2442 | return BinaryOperator::createLShr(Op0, | 
| Zhou Sheng | 222d5eb | 2007-03-25 05:01:29 +0000 | [diff] [blame] | 2443 | ConstantInt::get(Op0->getType(), C->getValue().logBase2())); | 
| Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2444 | } | 
|  | 2445 |  | 
|  | 2446 | // X udiv (C1 << N), where C1 is "1<<C2"  -->  X >> (N+C2) | 
| Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2447 | if (BinaryOperator *RHSI = dyn_cast<BinaryOperator>(I.getOperand(1))) { | 
| Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2448 | if (RHSI->getOpcode() == Instruction::Shl && | 
|  | 2449 | isa<ConstantInt>(RHSI->getOperand(0))) { | 
| Zhou Sheng | 150f3bb | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2450 | const APInt& C1 = cast<ConstantInt>(RHSI->getOperand(0))->getValue(); | 
| Reid Spencer | 6d39206 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2451 | if (C1.isPowerOf2()) { | 
| Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2452 | Value *N = RHSI->getOperand(1); | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2453 | const Type *NTy = N->getType(); | 
| Reid Spencer | 959a21d | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 2454 | if (uint32_t C2 = C1.logBase2()) { | 
| Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2455 | Constant *C2V = ConstantInt::get(NTy, C2); | 
|  | 2456 | N = InsertNewInstBefore(BinaryOperator::createAdd(N, C2V, "tmp"), I); | 
| Chris Lattner | 2e90b73 | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 2457 | } | 
| Reid Spencer | 0d5f923 | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 2458 | return BinaryOperator::createLShr(Op0, N); | 
| Chris Lattner | 2e90b73 | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 2459 | } | 
|  | 2460 | } | 
| Chris Lattner | dd0c174 | 2005-11-05 07:40:31 +0000 | [diff] [blame] | 2461 | } | 
|  | 2462 |  | 
| Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2463 | // udiv X, (Select Cond, C1, C2) --> Select Cond, (shr X, C1), (shr X, C2) | 
|  | 2464 | // where C1&C2 are powers of two. | 
| Reid Spencer | 3939b1a | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 2465 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) | 
| Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2466 | if (ConstantInt *STO = dyn_cast<ConstantInt>(SI->getOperand(1))) | 
| Reid Spencer | 3939b1a | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 2467 | if (ConstantInt *SFO = dyn_cast<ConstantInt>(SI->getOperand(2)))  { | 
| Zhou Sheng | 150f3bb | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2468 | const APInt &TVA = STO->getValue(), &FVA = SFO->getValue(); | 
| Reid Spencer | 6d39206 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2469 | if (TVA.isPowerOf2() && FVA.isPowerOf2()) { | 
| Reid Spencer | 3939b1a | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 2470 | // Compute the shift amounts | 
| Reid Spencer | 6d39206 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2471 | uint32_t TSA = TVA.logBase2(), FSA = FVA.logBase2(); | 
| Reid Spencer | 3939b1a | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 2472 | // Construct the "on true" case of the select | 
|  | 2473 | Constant *TC = ConstantInt::get(Op0->getType(), TSA); | 
|  | 2474 | Instruction *TSI = BinaryOperator::createLShr( | 
|  | 2475 | Op0, TC, SI->getName()+".t"); | 
|  | 2476 | TSI = InsertNewInstBefore(TSI, I); | 
|  | 2477 |  | 
|  | 2478 | // Construct the "on false" case of the select | 
|  | 2479 | Constant *FC = ConstantInt::get(Op0->getType(), FSA); | 
|  | 2480 | Instruction *FSI = BinaryOperator::createLShr( | 
|  | 2481 | Op0, FC, SI->getName()+".f"); | 
|  | 2482 | FSI = InsertNewInstBefore(FSI, I); | 
| Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2483 |  | 
| Reid Spencer | 3939b1a | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 2484 | // construct the select instruction and return it. | 
|  | 2485 | return new SelectInst(SI->getOperand(0), TSI, FSI, SI->getName()); | 
| Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2486 | } | 
| Reid Spencer | 3939b1a | 2007-03-05 23:36:13 +0000 | [diff] [blame] | 2487 | } | 
| Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2488 | return 0; | 
|  | 2489 | } | 
|  | 2490 |  | 
| Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2491 | Instruction *InstCombiner::visitSDiv(BinaryOperator &I) { | 
|  | 2492 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); | 
|  | 2493 |  | 
|  | 2494 | // Handle the integer div common cases | 
|  | 2495 | if (Instruction *Common = commonIDivTransforms(I)) | 
|  | 2496 | return Common; | 
|  | 2497 |  | 
|  | 2498 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { | 
|  | 2499 | // sdiv X, -1 == -X | 
|  | 2500 | if (RHS->isAllOnesValue()) | 
|  | 2501 | return BinaryOperator::createNeg(Op0); | 
|  | 2502 |  | 
|  | 2503 | // -X/C -> X/-C | 
|  | 2504 | if (Value *LHSNeg = dyn_castNegVal(Op0)) | 
|  | 2505 | return BinaryOperator::createSDiv(LHSNeg, ConstantExpr::getNeg(RHS)); | 
|  | 2506 | } | 
|  | 2507 |  | 
|  | 2508 | // If the sign bits of both operands are zero (i.e. we can prove they are | 
|  | 2509 | // unsigned inputs), turn this into a udiv. | 
| Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 2510 | if (I.getType()->isInteger()) { | 
| Reid Spencer | 6d39206 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2511 | APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits())); | 
| Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2512 | if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) { | 
|  | 2513 | return BinaryOperator::createUDiv(Op0, Op1, I.getName()); | 
|  | 2514 | } | 
|  | 2515 | } | 
|  | 2516 |  | 
|  | 2517 | return 0; | 
|  | 2518 | } | 
|  | 2519 |  | 
|  | 2520 | Instruction *InstCombiner::visitFDiv(BinaryOperator &I) { | 
|  | 2521 | return commonDivTransforms(I); | 
|  | 2522 | } | 
| Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2523 |  | 
| Chris Lattner | 85dda9a | 2006-03-02 06:50:58 +0000 | [diff] [blame] | 2524 | /// GetFactor - If we can prove that the specified value is at least a multiple | 
|  | 2525 | /// of some factor, return that factor. | 
|  | 2526 | static Constant *GetFactor(Value *V) { | 
|  | 2527 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) | 
|  | 2528 | return CI; | 
|  | 2529 |  | 
|  | 2530 | // Unless we can be tricky, we know this is a multiple of 1. | 
|  | 2531 | Constant *Result = ConstantInt::get(V->getType(), 1); | 
|  | 2532 |  | 
|  | 2533 | Instruction *I = dyn_cast<Instruction>(V); | 
|  | 2534 | if (!I) return Result; | 
|  | 2535 |  | 
|  | 2536 | if (I->getOpcode() == Instruction::Mul) { | 
|  | 2537 | // Handle multiplies by a constant, etc. | 
|  | 2538 | return ConstantExpr::getMul(GetFactor(I->getOperand(0)), | 
|  | 2539 | GetFactor(I->getOperand(1))); | 
|  | 2540 | } else if (I->getOpcode() == Instruction::Shl) { | 
|  | 2541 | // (X<<C) -> X * (1 << C) | 
|  | 2542 | if (Constant *ShRHS = dyn_cast<Constant>(I->getOperand(1))) { | 
|  | 2543 | ShRHS = ConstantExpr::getShl(Result, ShRHS); | 
|  | 2544 | return ConstantExpr::getMul(GetFactor(I->getOperand(0)), ShRHS); | 
|  | 2545 | } | 
|  | 2546 | } else if (I->getOpcode() == Instruction::And) { | 
|  | 2547 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) { | 
|  | 2548 | // X & 0xFFF0 is known to be a multiple of 16. | 
| Reid Spencer | a962d18 | 2007-03-24 00:42:08 +0000 | [diff] [blame] | 2549 | uint32_t Zeros = RHS->getValue().countTrailingZeros(); | 
| Chris Lattner | 85dda9a | 2006-03-02 06:50:58 +0000 | [diff] [blame] | 2550 | if (Zeros != V->getType()->getPrimitiveSizeInBits()) | 
|  | 2551 | return ConstantExpr::getShl(Result, | 
| Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2552 | ConstantInt::get(Result->getType(), Zeros)); | 
| Chris Lattner | 85dda9a | 2006-03-02 06:50:58 +0000 | [diff] [blame] | 2553 | } | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2554 | } else if (CastInst *CI = dyn_cast<CastInst>(I)) { | 
| Chris Lattner | 85dda9a | 2006-03-02 06:50:58 +0000 | [diff] [blame] | 2555 | // Only handle int->int casts. | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2556 | if (!CI->isIntegerCast()) | 
|  | 2557 | return Result; | 
|  | 2558 | Value *Op = CI->getOperand(0); | 
|  | 2559 | return ConstantExpr::getCast(CI->getOpcode(), GetFactor(Op), V->getType()); | 
| Chris Lattner | 85dda9a | 2006-03-02 06:50:58 +0000 | [diff] [blame] | 2560 | } | 
|  | 2561 | return Result; | 
|  | 2562 | } | 
|  | 2563 |  | 
| Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2564 | /// This function implements the transforms on rem instructions that work | 
|  | 2565 | /// regardless of the kind of rem instruction it is (urem, srem, or frem). It | 
|  | 2566 | /// is used by the visitors to those instructions. | 
|  | 2567 | /// @brief Transforms common to all three rem instructions | 
|  | 2568 | Instruction *InstCombiner::commonRemTransforms(BinaryOperator &I) { | 
| Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2569 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); | 
| Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2570 |  | 
| Chris Lattner | 0de4a8d | 2006-02-28 05:30:45 +0000 | [diff] [blame] | 2571 | // 0 % X == 0, we don't need to preserve faults! | 
|  | 2572 | if (Constant *LHS = dyn_cast<Constant>(Op0)) | 
|  | 2573 | if (LHS->isNullValue()) | 
|  | 2574 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); | 
|  | 2575 |  | 
|  | 2576 | if (isa<UndefValue>(Op0))              // undef % X -> 0 | 
|  | 2577 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); | 
|  | 2578 | if (isa<UndefValue>(Op1)) | 
|  | 2579 | return ReplaceInstUsesWith(I, Op1);  // X % undef -> undef | 
| Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2580 |  | 
|  | 2581 | // Handle cases involving: rem X, (select Cond, Y, Z) | 
|  | 2582 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) { | 
|  | 2583 | // rem X, (Cond ? 0 : Y) -> rem X, Y.  If the rem and the select are in | 
|  | 2584 | // the same basic block, then we replace the select with Y, and the | 
|  | 2585 | // condition of the select with false (if the cond value is in the same | 
|  | 2586 | // BB).  If the select has uses other than the div, this allows them to be | 
|  | 2587 | // simplified also. | 
|  | 2588 | if (Constant *ST = dyn_cast<Constant>(SI->getOperand(1))) | 
|  | 2589 | if (ST->isNullValue()) { | 
|  | 2590 | Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0)); | 
|  | 2591 | if (CondI && CondI->getParent() == I.getParent()) | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2592 | UpdateValueUsesWith(CondI, ConstantInt::getFalse()); | 
| Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2593 | else if (I.getParent() != SI->getParent() || SI->hasOneUse()) | 
|  | 2594 | I.setOperand(1, SI->getOperand(2)); | 
|  | 2595 | else | 
|  | 2596 | UpdateValueUsesWith(SI, SI->getOperand(2)); | 
| Chris Lattner | 7fd5f07 | 2004-07-06 07:01:22 +0000 | [diff] [blame] | 2597 | return &I; | 
|  | 2598 | } | 
| Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2599 | // Likewise for: rem X, (Cond ? Y : 0) -> rem X, Y | 
|  | 2600 | if (Constant *ST = dyn_cast<Constant>(SI->getOperand(2))) | 
|  | 2601 | if (ST->isNullValue()) { | 
|  | 2602 | Instruction *CondI = dyn_cast<Instruction>(SI->getOperand(0)); | 
|  | 2603 | if (CondI && CondI->getParent() == I.getParent()) | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2604 | UpdateValueUsesWith(CondI, ConstantInt::getTrue()); | 
| Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2605 | else if (I.getParent() != SI->getParent() || SI->hasOneUse()) | 
|  | 2606 | I.setOperand(1, SI->getOperand(1)); | 
|  | 2607 | else | 
|  | 2608 | UpdateValueUsesWith(SI, SI->getOperand(1)); | 
|  | 2609 | return &I; | 
|  | 2610 | } | 
| Chris Lattner | e9ff0ea | 2005-11-05 07:28:37 +0000 | [diff] [blame] | 2611 | } | 
| Chris Lattner | 7fd5f07 | 2004-07-06 07:01:22 +0000 | [diff] [blame] | 2612 |  | 
| Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2613 | return 0; | 
|  | 2614 | } | 
|  | 2615 |  | 
|  | 2616 | /// This function implements the transforms common to both integer remainder | 
|  | 2617 | /// instructions (urem and srem). It is called by the visitors to those integer | 
|  | 2618 | /// remainder instructions. | 
|  | 2619 | /// @brief Common integer remainder transforms | 
|  | 2620 | Instruction *InstCombiner::commonIRemTransforms(BinaryOperator &I) { | 
|  | 2621 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); | 
|  | 2622 |  | 
|  | 2623 | if (Instruction *common = commonRemTransforms(I)) | 
|  | 2624 | return common; | 
|  | 2625 |  | 
| Chris Lattner | bf5b7cf | 2004-12-12 21:48:58 +0000 | [diff] [blame] | 2626 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { | 
| Chris Lattner | 0de4a8d | 2006-02-28 05:30:45 +0000 | [diff] [blame] | 2627 | // X % 0 == undef, we don't need to preserve faults! | 
|  | 2628 | if (RHS->equalsInt(0)) | 
|  | 2629 | return ReplaceInstUsesWith(I, UndefValue::get(I.getType())); | 
|  | 2630 |  | 
| Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2631 | if (RHS->equalsInt(1))  // X % 1 == 0 | 
|  | 2632 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); | 
|  | 2633 |  | 
| Chris Lattner | b70f141 | 2006-02-28 05:49:21 +0000 | [diff] [blame] | 2634 | if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) { | 
|  | 2635 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0I)) { | 
|  | 2636 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) | 
|  | 2637 | return R; | 
|  | 2638 | } else if (isa<PHINode>(Op0I)) { | 
|  | 2639 | if (Instruction *NV = FoldOpIntoPhi(I)) | 
|  | 2640 | return NV; | 
| Chris Lattner | b70f141 | 2006-02-28 05:49:21 +0000 | [diff] [blame] | 2641 | } | 
| Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2642 | // (X * C1) % C2 --> 0  iff  C1 % C2 == 0 | 
|  | 2643 | if (ConstantExpr::getSRem(GetFactor(Op0I), RHS)->isNullValue()) | 
| Chris Lattner | 85dda9a | 2006-03-02 06:50:58 +0000 | [diff] [blame] | 2644 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); | 
| Chris Lattner | b70f141 | 2006-02-28 05:49:21 +0000 | [diff] [blame] | 2645 | } | 
| Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 2646 | } | 
|  | 2647 |  | 
| Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2648 | return 0; | 
|  | 2649 | } | 
|  | 2650 |  | 
|  | 2651 | Instruction *InstCombiner::visitURem(BinaryOperator &I) { | 
|  | 2652 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); | 
|  | 2653 |  | 
|  | 2654 | if (Instruction *common = commonIRemTransforms(I)) | 
|  | 2655 | return common; | 
|  | 2656 |  | 
|  | 2657 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { | 
|  | 2658 | // X urem C^2 -> X and C | 
|  | 2659 | // Check to see if this is an unsigned remainder with an exact power of 2, | 
|  | 2660 | // if so, convert to a bitwise and. | 
|  | 2661 | if (ConstantInt *C = dyn_cast<ConstantInt>(RHS)) | 
| Reid Spencer | 6d39206 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2662 | if (C->getValue().isPowerOf2()) | 
| Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2663 | return BinaryOperator::createAnd(Op0, SubOne(C)); | 
|  | 2664 | } | 
|  | 2665 |  | 
| Chris Lattner | 2e90b73 | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 2666 | if (Instruction *RHSI = dyn_cast<Instruction>(I.getOperand(1))) { | 
| Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2667 | // Turn A % (C << N), where C is 2^k, into A & ((C << N)-1) | 
|  | 2668 | if (RHSI->getOpcode() == Instruction::Shl && | 
|  | 2669 | isa<ConstantInt>(RHSI->getOperand(0))) { | 
| Zhou Sheng | 222d5eb | 2007-03-25 05:01:29 +0000 | [diff] [blame] | 2670 | if (cast<ConstantInt>(RHSI->getOperand(0))->getValue().isPowerOf2()) { | 
| Chris Lattner | 2e90b73 | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 2671 | Constant *N1 = ConstantInt::getAllOnesValue(I.getType()); | 
|  | 2672 | Value *Add = InsertNewInstBefore(BinaryOperator::createAdd(RHSI, N1, | 
|  | 2673 | "tmp"), I); | 
|  | 2674 | return BinaryOperator::createAnd(Op0, Add); | 
|  | 2675 | } | 
|  | 2676 | } | 
| Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2677 | } | 
| Chris Lattner | d79dc79 | 2006-09-09 20:26:32 +0000 | [diff] [blame] | 2678 |  | 
| Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2679 | // urem X, (select Cond, 2^C1, 2^C2) --> select Cond, (and X, C1), (and X, C2) | 
|  | 2680 | // where C1&C2 are powers of two. | 
|  | 2681 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) { | 
|  | 2682 | if (ConstantInt *STO = dyn_cast<ConstantInt>(SI->getOperand(1))) | 
|  | 2683 | if (ConstantInt *SFO = dyn_cast<ConstantInt>(SI->getOperand(2))) { | 
|  | 2684 | // STO == 0 and SFO == 0 handled above. | 
| Reid Spencer | 6d39206 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2685 | if ((STO->getValue().isPowerOf2()) && | 
|  | 2686 | (SFO->getValue().isPowerOf2())) { | 
| Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2687 | Value *TrueAnd = InsertNewInstBefore( | 
|  | 2688 | BinaryOperator::createAnd(Op0, SubOne(STO), SI->getName()+".t"), I); | 
|  | 2689 | Value *FalseAnd = InsertNewInstBefore( | 
|  | 2690 | BinaryOperator::createAnd(Op0, SubOne(SFO), SI->getName()+".f"), I); | 
|  | 2691 | return new SelectInst(SI->getOperand(0), TrueAnd, FalseAnd); | 
|  | 2692 | } | 
|  | 2693 | } | 
| Chris Lattner | 2e90b73 | 2006-02-05 07:54:04 +0000 | [diff] [blame] | 2694 | } | 
|  | 2695 |  | 
| Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 2696 | return 0; | 
|  | 2697 | } | 
|  | 2698 |  | 
| Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2699 | Instruction *InstCombiner::visitSRem(BinaryOperator &I) { | 
|  | 2700 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); | 
|  | 2701 |  | 
|  | 2702 | if (Instruction *common = commonIRemTransforms(I)) | 
|  | 2703 | return common; | 
|  | 2704 |  | 
|  | 2705 | if (Value *RHSNeg = dyn_castNegVal(Op1)) | 
|  | 2706 | if (!isa<ConstantInt>(RHSNeg) || | 
| Zhou Sheng | 222d5eb | 2007-03-25 05:01:29 +0000 | [diff] [blame] | 2707 | cast<ConstantInt>(RHSNeg)->getValue().isStrictlyPositive()) { | 
| Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2708 | // X % -Y -> X % Y | 
|  | 2709 | AddUsesToWorkList(I); | 
|  | 2710 | I.setOperand(1, RHSNeg); | 
|  | 2711 | return &I; | 
|  | 2712 | } | 
|  | 2713 |  | 
|  | 2714 | // If the top bits of both operands are zero (i.e. we can prove they are | 
|  | 2715 | // unsigned inputs), turn this into a urem. | 
| Reid Spencer | 6d39206 | 2007-03-23 20:05:17 +0000 | [diff] [blame] | 2716 | APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits())); | 
| Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2717 | if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) { | 
|  | 2718 | // X srem Y -> X urem Y, iff X and Y don't have sign bit set | 
|  | 2719 | return BinaryOperator::createURem(Op0, Op1, I.getName()); | 
|  | 2720 | } | 
|  | 2721 |  | 
|  | 2722 | return 0; | 
|  | 2723 | } | 
|  | 2724 |  | 
|  | 2725 | Instruction *InstCombiner::visitFRem(BinaryOperator &I) { | 
| Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2726 | return commonRemTransforms(I); | 
|  | 2727 | } | 
|  | 2728 |  | 
| Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2729 | // isMaxValueMinusOne - return true if this is Max-1 | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2730 | static bool isMaxValueMinusOne(const ConstantInt *C, bool isSigned) { | 
| Reid Spencer | ef599b0 | 2007-03-19 21:10:28 +0000 | [diff] [blame] | 2731 | uint32_t TypeBits = C->getType()->getPrimitiveSizeInBits(); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2732 | if (isSigned) { | 
|  | 2733 | // Calculate 0111111111..11111 | 
| Reid Spencer | ef599b0 | 2007-03-19 21:10:28 +0000 | [diff] [blame] | 2734 | APInt Val(APInt::getSignedMaxValue(TypeBits)); | 
|  | 2735 | return C->getValue() == Val-1; | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2736 | } | 
| Reid Spencer | ef599b0 | 2007-03-19 21:10:28 +0000 | [diff] [blame] | 2737 | return C->getValue() == APInt::getAllOnesValue(TypeBits) - 1; | 
| Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2738 | } | 
|  | 2739 |  | 
|  | 2740 | // isMinValuePlusOne - return true if this is Min+1 | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2741 | static bool isMinValuePlusOne(const ConstantInt *C, bool isSigned) { | 
|  | 2742 | if (isSigned) { | 
|  | 2743 | // Calculate 1111111111000000000000 | 
| Reid Spencer | 3b93db7 | 2007-03-19 21:08:07 +0000 | [diff] [blame] | 2744 | uint32_t TypeBits = C->getType()->getPrimitiveSizeInBits(); | 
|  | 2745 | APInt Val(APInt::getSignedMinValue(TypeBits)); | 
|  | 2746 | return C->getValue() == Val+1; | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2747 | } | 
| Reid Spencer | 3b93db7 | 2007-03-19 21:08:07 +0000 | [diff] [blame] | 2748 | return C->getValue() == 1; // unsigned | 
| Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 2749 | } | 
|  | 2750 |  | 
| Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 2751 | // isOneBitSet - Return true if there is exactly one bit set in the specified | 
|  | 2752 | // constant. | 
|  | 2753 | static bool isOneBitSet(const ConstantInt *CI) { | 
| Reid Spencer | 6682721 | 2007-03-20 00:16:52 +0000 | [diff] [blame] | 2754 | return CI->getValue().isPowerOf2(); | 
| Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 2755 | } | 
|  | 2756 |  | 
| Chris Lattner | 8fc5af4 | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 2757 | // isHighOnes - Return true if the constant is of the form 1+0+. | 
|  | 2758 | // This is the same as lowones(~X). | 
|  | 2759 | static bool isHighOnes(const ConstantInt *CI) { | 
| Zhou Sheng | b394934 | 2007-03-20 12:49:06 +0000 | [diff] [blame] | 2760 | return (~CI->getValue() + 1).isPowerOf2(); | 
| Chris Lattner | 8fc5af4 | 2004-09-23 21:46:38 +0000 | [diff] [blame] | 2761 | } | 
|  | 2762 |  | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2763 | /// getICmpCode - Encode a icmp predicate into a three bit mask.  These bits | 
| Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2764 | /// are carefully arranged to allow folding of expressions such as: | 
|  | 2765 | /// | 
|  | 2766 | ///      (A < B) | (A > B) --> (A != B) | 
|  | 2767 | /// | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2768 | /// Note that this is only valid if the first and second predicates have the | 
|  | 2769 | /// same sign. Is illegal to do: (A u< B) | (A s> B) | 
| Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2770 | /// | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2771 | /// Three bits are used to represent the condition, as follows: | 
|  | 2772 | ///   0  A > B | 
|  | 2773 | ///   1  A == B | 
|  | 2774 | ///   2  A < B | 
|  | 2775 | /// | 
|  | 2776 | /// <=>  Value  Definition | 
|  | 2777 | /// 000     0   Always false | 
|  | 2778 | /// 001     1   A >  B | 
|  | 2779 | /// 010     2   A == B | 
|  | 2780 | /// 011     3   A >= B | 
|  | 2781 | /// 100     4   A <  B | 
|  | 2782 | /// 101     5   A != B | 
|  | 2783 | /// 110     6   A <= B | 
|  | 2784 | /// 111     7   Always true | 
|  | 2785 | /// | 
|  | 2786 | static unsigned getICmpCode(const ICmpInst *ICI) { | 
|  | 2787 | switch (ICI->getPredicate()) { | 
| Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2788 | // False -> 0 | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2789 | case ICmpInst::ICMP_UGT: return 1;  // 001 | 
|  | 2790 | case ICmpInst::ICMP_SGT: return 1;  // 001 | 
|  | 2791 | case ICmpInst::ICMP_EQ:  return 2;  // 010 | 
|  | 2792 | case ICmpInst::ICMP_UGE: return 3;  // 011 | 
|  | 2793 | case ICmpInst::ICMP_SGE: return 3;  // 011 | 
|  | 2794 | case ICmpInst::ICMP_ULT: return 4;  // 100 | 
|  | 2795 | case ICmpInst::ICMP_SLT: return 4;  // 100 | 
|  | 2796 | case ICmpInst::ICMP_NE:  return 5;  // 101 | 
|  | 2797 | case ICmpInst::ICMP_ULE: return 6;  // 110 | 
|  | 2798 | case ICmpInst::ICMP_SLE: return 6;  // 110 | 
| Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2799 | // True -> 7 | 
|  | 2800 | default: | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2801 | assert(0 && "Invalid ICmp predicate!"); | 
| Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2802 | return 0; | 
|  | 2803 | } | 
|  | 2804 | } | 
|  | 2805 |  | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2806 | /// getICmpValue - This is the complement of getICmpCode, which turns an | 
|  | 2807 | /// opcode and two operands into either a constant true or false, or a brand | 
|  | 2808 | /// new /// ICmp instruction. The sign is passed in to determine which kind | 
|  | 2809 | /// of predicate to use in new icmp instructions. | 
|  | 2810 | static Value *getICmpValue(bool sign, unsigned code, Value *LHS, Value *RHS) { | 
|  | 2811 | switch (code) { | 
|  | 2812 | default: assert(0 && "Illegal ICmp code!"); | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2813 | case  0: return ConstantInt::getFalse(); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2814 | case  1: | 
|  | 2815 | if (sign) | 
|  | 2816 | return new ICmpInst(ICmpInst::ICMP_SGT, LHS, RHS); | 
|  | 2817 | else | 
|  | 2818 | return new ICmpInst(ICmpInst::ICMP_UGT, LHS, RHS); | 
|  | 2819 | case  2: return new ICmpInst(ICmpInst::ICMP_EQ,  LHS, RHS); | 
|  | 2820 | case  3: | 
|  | 2821 | if (sign) | 
|  | 2822 | return new ICmpInst(ICmpInst::ICMP_SGE, LHS, RHS); | 
|  | 2823 | else | 
|  | 2824 | return new ICmpInst(ICmpInst::ICMP_UGE, LHS, RHS); | 
|  | 2825 | case  4: | 
|  | 2826 | if (sign) | 
|  | 2827 | return new ICmpInst(ICmpInst::ICMP_SLT, LHS, RHS); | 
|  | 2828 | else | 
|  | 2829 | return new ICmpInst(ICmpInst::ICMP_ULT, LHS, RHS); | 
|  | 2830 | case  5: return new ICmpInst(ICmpInst::ICMP_NE,  LHS, RHS); | 
|  | 2831 | case  6: | 
|  | 2832 | if (sign) | 
|  | 2833 | return new ICmpInst(ICmpInst::ICMP_SLE, LHS, RHS); | 
|  | 2834 | else | 
|  | 2835 | return new ICmpInst(ICmpInst::ICMP_ULE, LHS, RHS); | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2836 | case  7: return ConstantInt::getTrue(); | 
| Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2837 | } | 
|  | 2838 | } | 
|  | 2839 |  | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2840 | static bool PredicatesFoldable(ICmpInst::Predicate p1, ICmpInst::Predicate p2) { | 
|  | 2841 | return (ICmpInst::isSignedPredicate(p1) == ICmpInst::isSignedPredicate(p2)) || | 
|  | 2842 | (ICmpInst::isSignedPredicate(p1) && | 
|  | 2843 | (p2 == ICmpInst::ICMP_EQ || p2 == ICmpInst::ICMP_NE)) || | 
|  | 2844 | (ICmpInst::isSignedPredicate(p2) && | 
|  | 2845 | (p1 == ICmpInst::ICMP_EQ || p1 == ICmpInst::ICMP_NE)); | 
|  | 2846 | } | 
|  | 2847 |  | 
|  | 2848 | namespace { | 
|  | 2849 | // FoldICmpLogical - Implements (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B) | 
|  | 2850 | struct FoldICmpLogical { | 
| Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2851 | InstCombiner &IC; | 
|  | 2852 | Value *LHS, *RHS; | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2853 | ICmpInst::Predicate pred; | 
|  | 2854 | FoldICmpLogical(InstCombiner &ic, ICmpInst *ICI) | 
|  | 2855 | : IC(ic), LHS(ICI->getOperand(0)), RHS(ICI->getOperand(1)), | 
|  | 2856 | pred(ICI->getPredicate()) {} | 
| Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2857 | bool shouldApply(Value *V) const { | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2858 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(V)) | 
|  | 2859 | if (PredicatesFoldable(pred, ICI->getPredicate())) | 
|  | 2860 | return (ICI->getOperand(0) == LHS && ICI->getOperand(1) == RHS || | 
|  | 2861 | ICI->getOperand(0) == RHS && ICI->getOperand(1) == LHS); | 
| Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2862 | return false; | 
|  | 2863 | } | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2864 | Instruction *apply(Instruction &Log) const { | 
|  | 2865 | ICmpInst *ICI = cast<ICmpInst>(Log.getOperand(0)); | 
|  | 2866 | if (ICI->getOperand(0) != LHS) { | 
|  | 2867 | assert(ICI->getOperand(1) == LHS); | 
|  | 2868 | ICI->swapOperands();  // Swap the LHS and RHS of the ICmp | 
| Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2869 | } | 
|  | 2870 |  | 
| Chris Lattner | d1bce95 | 2007-03-13 14:27:42 +0000 | [diff] [blame] | 2871 | ICmpInst *RHSICI = cast<ICmpInst>(Log.getOperand(1)); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2872 | unsigned LHSCode = getICmpCode(ICI); | 
| Chris Lattner | d1bce95 | 2007-03-13 14:27:42 +0000 | [diff] [blame] | 2873 | unsigned RHSCode = getICmpCode(RHSICI); | 
| Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2874 | unsigned Code; | 
|  | 2875 | switch (Log.getOpcode()) { | 
|  | 2876 | case Instruction::And: Code = LHSCode & RHSCode; break; | 
|  | 2877 | case Instruction::Or:  Code = LHSCode | RHSCode; break; | 
|  | 2878 | case Instruction::Xor: Code = LHSCode ^ RHSCode; break; | 
| Chris Lattner | 2caaaba | 2003-09-22 20:33:34 +0000 | [diff] [blame] | 2879 | default: assert(0 && "Illegal logical opcode!"); return 0; | 
| Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2880 | } | 
|  | 2881 |  | 
| Chris Lattner | d1bce95 | 2007-03-13 14:27:42 +0000 | [diff] [blame] | 2882 | bool isSigned = ICmpInst::isSignedPredicate(RHSICI->getPredicate()) || | 
|  | 2883 | ICmpInst::isSignedPredicate(ICI->getPredicate()); | 
|  | 2884 |  | 
|  | 2885 | Value *RV = getICmpValue(isSigned, Code, LHS, RHS); | 
| Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2886 | if (Instruction *I = dyn_cast<Instruction>(RV)) | 
|  | 2887 | return I; | 
|  | 2888 | // Otherwise, it's a constant boolean value... | 
|  | 2889 | return IC.ReplaceInstUsesWith(Log, RV); | 
|  | 2890 | } | 
|  | 2891 | }; | 
| Chris Lattner | e3a63d1 | 2006-11-15 04:53:24 +0000 | [diff] [blame] | 2892 | } // end anonymous namespace | 
| Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 2893 |  | 
| Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2894 | // OptAndOp - This handles expressions of the form ((val OP C1) & C2).  Where | 
|  | 2895 | // the Op parameter is 'OP', OpRHS is 'C1', and AndRHS is 'C2'.  Op is | 
| Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2896 | // guaranteed to be a binary operator. | 
| Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2897 | Instruction *InstCombiner::OptAndOp(Instruction *Op, | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2898 | ConstantInt *OpRHS, | 
|  | 2899 | ConstantInt *AndRHS, | 
| Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2900 | BinaryOperator &TheAnd) { | 
|  | 2901 | Value *X = Op->getOperand(0); | 
| Chris Lattner | fcf21a7 | 2004-01-12 19:47:05 +0000 | [diff] [blame] | 2902 | Constant *Together = 0; | 
| Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2903 | if (!Op->isShift()) | 
| Reid Spencer | 80263aa | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 2904 | Together = And(AndRHS, OpRHS); | 
| Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 2905 |  | 
| Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2906 | switch (Op->getOpcode()) { | 
|  | 2907 | case Instruction::Xor: | 
| Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2908 | if (Op->hasOneUse()) { | 
| Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2909 | // (X ^ C1) & C2 --> (X & C2) ^ (C1&C2) | 
| Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 2910 | Instruction *And = BinaryOperator::createAnd(X, AndRHS); | 
| Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2911 | InsertNewInstBefore(And, TheAnd); | 
| Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 2912 | And->takeName(Op); | 
| Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2913 | return BinaryOperator::createXor(And, Together); | 
| Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2914 | } | 
|  | 2915 | break; | 
|  | 2916 | case Instruction::Or: | 
| Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2917 | if (Together == AndRHS) // (X | C) & C --> C | 
|  | 2918 | return ReplaceInstUsesWith(TheAnd, AndRHS); | 
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2919 |  | 
| Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2920 | if (Op->hasOneUse() && Together != OpRHS) { | 
|  | 2921 | // (X | C1) & C2 --> (X | (C1&C2)) & C2 | 
| Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 2922 | Instruction *Or = BinaryOperator::createOr(X, Together); | 
| Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2923 | InsertNewInstBefore(Or, TheAnd); | 
| Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 2924 | Or->takeName(Op); | 
| Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 2925 | return BinaryOperator::createAnd(Or, AndRHS); | 
| Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2926 | } | 
|  | 2927 | break; | 
|  | 2928 | case Instruction::Add: | 
| Chris Lattner | f95d9b9 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 2929 | if (Op->hasOneUse()) { | 
| Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2930 | // Adding a one to a single bit bit-field should be turned into an XOR | 
|  | 2931 | // of the bit.  First thing to check is to see if this AND is with a | 
|  | 2932 | // single bit constant. | 
| Zhou Sheng | 150f3bb | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2933 | const APInt& AndRHSV = cast<ConstantInt>(AndRHS)->getValue(); | 
| Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2934 |  | 
|  | 2935 | // If there is only one bit set... | 
| Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 2936 | if (isOneBitSet(cast<ConstantInt>(AndRHS))) { | 
| Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2937 | // Ok, at this point, we know that we are masking the result of the | 
|  | 2938 | // ADD down to exactly one bit.  If the constant we are adding has | 
|  | 2939 | // no bits set below this bit, then we can eliminate the ADD. | 
| Zhou Sheng | 150f3bb | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 2940 | const APInt& AddRHS = cast<ConstantInt>(OpRHS)->getValue(); | 
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2941 |  | 
| Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2942 | // Check to see if any bits below the one bit set in AndRHSV are set. | 
|  | 2943 | if ((AddRHS & (AndRHSV-1)) == 0) { | 
|  | 2944 | // If not, the only thing that can effect the output of the AND is | 
|  | 2945 | // the bit specified by AndRHSV.  If that bit is set, the effect of | 
|  | 2946 | // the XOR is to toggle the bit.  If it is clear, then the ADD has | 
|  | 2947 | // no effect. | 
|  | 2948 | if ((AddRHS & AndRHSV) == 0) { // Bit is not set, noop | 
|  | 2949 | TheAnd.setOperand(0, X); | 
|  | 2950 | return &TheAnd; | 
|  | 2951 | } else { | 
| Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2952 | // Pull the XOR out of the AND. | 
| Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 2953 | Instruction *NewAnd = BinaryOperator::createAnd(X, AndRHS); | 
| Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2954 | InsertNewInstBefore(NewAnd, TheAnd); | 
| Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 2955 | NewAnd->takeName(Op); | 
| Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 2956 | return BinaryOperator::createXor(NewAnd, AndRHS); | 
| Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 2957 | } | 
|  | 2958 | } | 
|  | 2959 | } | 
|  | 2960 | } | 
|  | 2961 | break; | 
| Chris Lattner | 2da2917 | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 2962 |  | 
|  | 2963 | case Instruction::Shl: { | 
|  | 2964 | // We know that the AND will not produce any of the bits shifted in, so if | 
|  | 2965 | // the anded constant includes them, clear them now! | 
|  | 2966 | // | 
| Zhou Sheng | b3a80b1 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 2967 | uint32_t BitWidth = AndRHS->getType()->getBitWidth(); | 
| Zhou Sheng | b25806f | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 2968 | uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth); | 
| Zhou Sheng | b3a80b1 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 2969 | APInt ShlMask(APInt::getHighBitsSet(BitWidth, BitWidth-OpRHSVal)); | 
|  | 2970 | ConstantInt *CI = ConstantInt::get(AndRHS->getValue() & ShlMask); | 
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2971 |  | 
| Zhou Sheng | b3a80b1 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 2972 | if (CI->getValue() == ShlMask) { | 
|  | 2973 | // Masking out bits that the shift already masks | 
| Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 2974 | return ReplaceInstUsesWith(TheAnd, Op);   // No need for the and. | 
|  | 2975 | } else if (CI != AndRHS) {                  // Reducing bits set in and. | 
| Chris Lattner | 2da2917 | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 2976 | TheAnd.setOperand(1, CI); | 
|  | 2977 | return &TheAnd; | 
|  | 2978 | } | 
|  | 2979 | break; | 
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2980 | } | 
| Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2981 | case Instruction::LShr: | 
|  | 2982 | { | 
| Chris Lattner | 2da2917 | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 2983 | // We know that the AND will not produce any of the bits shifted in, so if | 
|  | 2984 | // the anded constant includes them, clear them now!  This only applies to | 
|  | 2985 | // unsigned shifts, because a signed shr may bring in set bits! | 
|  | 2986 | // | 
| Zhou Sheng | b3a80b1 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 2987 | uint32_t BitWidth = AndRHS->getType()->getBitWidth(); | 
| Zhou Sheng | b25806f | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 2988 | uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth); | 
| Zhou Sheng | b3a80b1 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 2989 | APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal)); | 
|  | 2990 | ConstantInt *CI = ConstantInt::get(AndRHS->getValue() & ShrMask); | 
| Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 2991 |  | 
| Zhou Sheng | b3a80b1 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 2992 | if (CI->getValue() == ShrMask) { | 
|  | 2993 | // Masking out bits that the shift already masks. | 
| Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 2994 | return ReplaceInstUsesWith(TheAnd, Op); | 
|  | 2995 | } else if (CI != AndRHS) { | 
|  | 2996 | TheAnd.setOperand(1, CI);  // Reduce bits set in and cst. | 
|  | 2997 | return &TheAnd; | 
|  | 2998 | } | 
|  | 2999 | break; | 
|  | 3000 | } | 
|  | 3001 | case Instruction::AShr: | 
|  | 3002 | // Signed shr. | 
|  | 3003 | // See if this is shifting in some sign extension, then masking it out | 
|  | 3004 | // with an and. | 
|  | 3005 | if (Op->hasOneUse()) { | 
| Zhou Sheng | b3a80b1 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3006 | uint32_t BitWidth = AndRHS->getType()->getBitWidth(); | 
| Zhou Sheng | b25806f | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3007 | uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth); | 
| Zhou Sheng | b3a80b1 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3008 | APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal)); | 
|  | 3009 | Constant *C = ConstantInt::get(AndRHS->getValue() & ShrMask); | 
| Reid Spencer | 2a499b0 | 2006-12-13 17:19:09 +0000 | [diff] [blame] | 3010 | if (C == AndRHS) {          // Masking out bits shifted in. | 
| Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 3011 | // (Val ashr C1) & C2 -> (Val lshr C1) & C2 | 
| Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 3012 | // Make the argument unsigned. | 
|  | 3013 | Value *ShVal = Op->getOperand(0); | 
| Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3014 | ShVal = InsertNewInstBefore( | 
| Reid Spencer | 0d5f923 | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 3015 | BinaryOperator::createLShr(ShVal, OpRHS, | 
| Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3016 | Op->getName()), TheAnd); | 
| Reid Spencer | 2a499b0 | 2006-12-13 17:19:09 +0000 | [diff] [blame] | 3017 | return BinaryOperator::createAnd(ShVal, AndRHS, TheAnd.getName()); | 
| Chris Lattner | 7e79427 | 2004-09-24 15:21:34 +0000 | [diff] [blame] | 3018 | } | 
| Chris Lattner | 2da2917 | 2003-09-19 19:05:02 +0000 | [diff] [blame] | 3019 | } | 
|  | 3020 | break; | 
| Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3021 | } | 
|  | 3022 | return 0; | 
|  | 3023 | } | 
|  | 3024 |  | 
| Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 3025 |  | 
| Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3026 | /// InsertRangeTest - Emit a computation of: (V >= Lo && V < Hi) if Inside is | 
|  | 3027 | /// true, otherwise (V < Lo || V >= Hi).  In pratice, we emit the more efficient | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3028 | /// (V-Lo) <u Hi-Lo.  This method expects that Lo <= Hi. isSigned indicates | 
|  | 3029 | /// whether to treat the V, Lo and HI as signed or not. IB is the location to | 
| Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3030 | /// insert new instructions. | 
|  | 3031 | Instruction *InstCombiner::InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3032 | bool isSigned, bool Inside, | 
|  | 3033 | Instruction &IB) { | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3034 | assert(cast<ConstantInt>(ConstantExpr::getICmp((isSigned ? | 
| Reid Spencer | cddc9df | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 3035 | ICmpInst::ICMP_SLE:ICmpInst::ICMP_ULE), Lo, Hi))->getZExtValue() && | 
| Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3036 | "Lo is not <= Hi in range emission code!"); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3037 |  | 
| Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3038 | if (Inside) { | 
|  | 3039 | if (Lo == Hi)  // Trivially false. | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3040 | return new ICmpInst(ICmpInst::ICMP_NE, V, V); | 
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3041 |  | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3042 | // V >= Min && V < Hi --> V < Hi | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3043 | if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) { | 
| Reid Spencer | f407116 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 3044 | ICmpInst::Predicate pred = (isSigned ? | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3045 | ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT); | 
|  | 3046 | return new ICmpInst(pred, V, Hi); | 
|  | 3047 | } | 
|  | 3048 |  | 
|  | 3049 | // Emit V-Lo <u Hi-Lo | 
|  | 3050 | Constant *NegLo = ConstantExpr::getNeg(Lo); | 
|  | 3051 | Instruction *Add = BinaryOperator::createAdd(V, NegLo, V->getName()+".off"); | 
| Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3052 | InsertNewInstBefore(Add, IB); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3053 | Constant *UpperBound = ConstantExpr::getAdd(NegLo, Hi); | 
|  | 3054 | return new ICmpInst(ICmpInst::ICMP_ULT, Add, UpperBound); | 
| Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3055 | } | 
|  | 3056 |  | 
|  | 3057 | if (Lo == Hi)  // Trivially true. | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3058 | return new ICmpInst(ICmpInst::ICMP_EQ, V, V); | 
| Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3059 |  | 
| Reid Spencer | f407116 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 3060 | // V < Min || V >= Hi -> V > Hi-1 | 
| Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3061 | Hi = SubOne(cast<ConstantInt>(Hi)); | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3062 | if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) { | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3063 | ICmpInst::Predicate pred = (isSigned ? | 
|  | 3064 | ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT); | 
|  | 3065 | return new ICmpInst(pred, V, Hi); | 
|  | 3066 | } | 
| Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3067 |  | 
| Reid Spencer | f407116 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 3068 | // Emit V-Lo >u Hi-1-Lo | 
|  | 3069 | // Note that Hi has already had one subtracted from it, above. | 
|  | 3070 | ConstantInt *NegLo = cast<ConstantInt>(ConstantExpr::getNeg(Lo)); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3071 | Instruction *Add = BinaryOperator::createAdd(V, NegLo, V->getName()+".off"); | 
| Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3072 | InsertNewInstBefore(Add, IB); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3073 | Constant *LowerBound = ConstantExpr::getAdd(NegLo, Hi); | 
|  | 3074 | return new ICmpInst(ICmpInst::ICMP_UGT, Add, LowerBound); | 
| Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 3075 | } | 
|  | 3076 |  | 
| Chris Lattner | b4b2530 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3077 | // isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with | 
|  | 3078 | // any number of 0s on either side.  The 1s are allowed to wrap from LSB to | 
|  | 3079 | // MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs.  0x0F0F0000 is | 
|  | 3080 | // not, since all 1s are not contiguous. | 
| Zhou Sheng | 56cda95 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 3081 | static bool isRunOfOnes(ConstantInt *Val, uint32_t &MB, uint32_t &ME) { | 
| Zhou Sheng | 150f3bb | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 3082 | const APInt& V = Val->getValue(); | 
| Reid Spencer | a962d18 | 2007-03-24 00:42:08 +0000 | [diff] [blame] | 3083 | uint32_t BitWidth = Val->getType()->getBitWidth(); | 
|  | 3084 | if (!APIntOps::isShiftedMask(BitWidth, V)) return false; | 
| Chris Lattner | b4b2530 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3085 |  | 
|  | 3086 | // look for the first zero bit after the run of ones | 
| Reid Spencer | a962d18 | 2007-03-24 00:42:08 +0000 | [diff] [blame] | 3087 | MB = BitWidth - ((V - 1) ^ V).countLeadingZeros(); | 
| Chris Lattner | b4b2530 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3088 | // look for the first non-zero bit | 
| Reid Spencer | a962d18 | 2007-03-24 00:42:08 +0000 | [diff] [blame] | 3089 | ME = V.getActiveBits(); | 
| Chris Lattner | b4b2530 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3090 | return true; | 
|  | 3091 | } | 
|  | 3092 |  | 
| Chris Lattner | b4b2530 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3093 | /// FoldLogicalPlusAnd - This is part of an expression (LHS +/- RHS) & Mask, | 
|  | 3094 | /// where isSub determines whether the operator is a sub.  If we can fold one of | 
|  | 3095 | /// the following xforms: | 
| Chris Lattner | af51757 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3096 | /// | 
|  | 3097 | /// ((A & N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == Mask | 
|  | 3098 | /// ((A | N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0 | 
|  | 3099 | /// ((A ^ N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0 | 
|  | 3100 | /// | 
|  | 3101 | /// return (A +/- B). | 
|  | 3102 | /// | 
|  | 3103 | Value *InstCombiner::FoldLogicalPlusAnd(Value *LHS, Value *RHS, | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3104 | ConstantInt *Mask, bool isSub, | 
| Chris Lattner | af51757 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3105 | Instruction &I) { | 
|  | 3106 | Instruction *LHSI = dyn_cast<Instruction>(LHS); | 
|  | 3107 | if (!LHSI || LHSI->getNumOperands() != 2 || | 
|  | 3108 | !isa<ConstantInt>(LHSI->getOperand(1))) return 0; | 
|  | 3109 |  | 
|  | 3110 | ConstantInt *N = cast<ConstantInt>(LHSI->getOperand(1)); | 
|  | 3111 |  | 
|  | 3112 | switch (LHSI->getOpcode()) { | 
|  | 3113 | default: return 0; | 
|  | 3114 | case Instruction::And: | 
| Reid Spencer | 80263aa | 2007-03-25 05:33:51 +0000 | [diff] [blame] | 3115 | if (And(N, Mask) == Mask) { | 
| Chris Lattner | b4b2530 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3116 | // If the AndRHS is a power of two minus one (0+1+), this is simple. | 
| Zhou Sheng | e9ebd3f | 2007-03-24 15:34:37 +0000 | [diff] [blame] | 3117 | if ((Mask->getValue().countLeadingZeros() + | 
|  | 3118 | Mask->getValue().countPopulation()) == | 
|  | 3119 | Mask->getValue().getBitWidth()) | 
| Chris Lattner | b4b2530 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3120 | break; | 
|  | 3121 |  | 
|  | 3122 | // Otherwise, if Mask is 0+1+0+, and if B is known to have the low 0+ | 
|  | 3123 | // part, we don't need any explicit masks to take them out of A.  If that | 
|  | 3124 | // is all N is, ignore it. | 
| Zhou Sheng | 56cda95 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 3125 | uint32_t MB = 0, ME = 0; | 
| Chris Lattner | b4b2530 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3126 | if (isRunOfOnes(Mask, MB, ME)) {  // begin/end bit of run, inclusive | 
| Reid Spencer | 6274c72 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 3127 | uint32_t BitWidth = cast<IntegerType>(RHS->getType())->getBitWidth(); | 
| Zhou Sheng | b3a80b1 | 2007-03-29 08:15:12 +0000 | [diff] [blame] | 3128 | APInt Mask(APInt::getLowBitsSet(BitWidth, MB-1)); | 
| Chris Lattner | c3ebf40 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 3129 | if (MaskedValueIsZero(RHS, Mask)) | 
| Chris Lattner | b4b2530 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3130 | break; | 
|  | 3131 | } | 
|  | 3132 | } | 
| Chris Lattner | af51757 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3133 | return 0; | 
|  | 3134 | case Instruction::Or: | 
|  | 3135 | case Instruction::Xor: | 
| Chris Lattner | b4b2530 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3136 | // If the AndRHS is a power of two minus one (0+1+), and N&Mask == 0 | 
| Zhou Sheng | e9ebd3f | 2007-03-24 15:34:37 +0000 | [diff] [blame] | 3137 | if ((Mask->getValue().countLeadingZeros() + | 
|  | 3138 | Mask->getValue().countPopulation()) == Mask->getValue().getBitWidth() | 
| Reid Spencer | 54d5b1b | 2007-03-26 23:58:26 +0000 | [diff] [blame] | 3139 | && And(N, Mask)->isZero()) | 
| Chris Lattner | af51757 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3140 | break; | 
|  | 3141 | return 0; | 
|  | 3142 | } | 
|  | 3143 |  | 
|  | 3144 | Instruction *New; | 
|  | 3145 | if (isSub) | 
|  | 3146 | New = BinaryOperator::createSub(LHSI->getOperand(0), RHS, "fold"); | 
|  | 3147 | else | 
|  | 3148 | New = BinaryOperator::createAdd(LHSI->getOperand(0), RHS, "fold"); | 
|  | 3149 | return InsertNewInstBefore(New, I); | 
|  | 3150 | } | 
|  | 3151 |  | 
| Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3152 | Instruction *InstCombiner::visitAnd(BinaryOperator &I) { | 
| Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 3153 | bool Changed = SimplifyCommutative(I); | 
| Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3154 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); | 
| Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3155 |  | 
| Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3156 | if (isa<UndefValue>(Op1))                         // X & undef -> 0 | 
|  | 3157 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); | 
|  | 3158 |  | 
| Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3159 | // and X, X = X | 
|  | 3160 | if (Op0 == Op1) | 
| Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 3161 | return ReplaceInstUsesWith(I, Op1); | 
| Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3162 |  | 
| Chris Lattner | 5b2edb1 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3163 | // See if we can simplify any instructions used by the instruction whose sole | 
| Chris Lattner | 5997cf9 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 3164 | // purpose is to compute bits we don't care about. | 
| Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 3165 | if (!isa<VectorType>(I.getType())) { | 
| Reid Spencer | b722f2b | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 3166 | uint32_t BitWidth = cast<IntegerType>(I.getType())->getBitWidth(); | 
|  | 3167 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); | 
|  | 3168 | if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth), | 
| Chris Lattner | 120ab03 | 2007-01-18 22:16:33 +0000 | [diff] [blame] | 3169 | KnownZero, KnownOne)) | 
| Reid Spencer | 54d5b1b | 2007-03-26 23:58:26 +0000 | [diff] [blame] | 3170 | return &I; | 
| Chris Lattner | 120ab03 | 2007-01-18 22:16:33 +0000 | [diff] [blame] | 3171 | } else { | 
| Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 3172 | if (ConstantVector *CP = dyn_cast<ConstantVector>(Op1)) { | 
| Chris Lattner | 120ab03 | 2007-01-18 22:16:33 +0000 | [diff] [blame] | 3173 | if (CP->isAllOnesValue()) | 
|  | 3174 | return ReplaceInstUsesWith(I, I.getOperand(0)); | 
|  | 3175 | } | 
|  | 3176 | } | 
| Chris Lattner | 5997cf9 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 3177 |  | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3178 | if (ConstantInt *AndRHS = dyn_cast<ConstantInt>(Op1)) { | 
| Zhou Sheng | 150f3bb | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 3179 | const APInt& AndRHSMask = AndRHS->getValue(); | 
|  | 3180 | APInt NotAndRHS(~AndRHSMask); | 
| Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3181 |  | 
| Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3182 | // Optimize a variety of ((val OP C1) & C2) combinations... | 
| Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3183 | if (isa<BinaryOperator>(Op0)) { | 
| Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3184 | Instruction *Op0I = cast<Instruction>(Op0); | 
| Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3185 | Value *Op0LHS = Op0I->getOperand(0); | 
|  | 3186 | Value *Op0RHS = Op0I->getOperand(1); | 
|  | 3187 | switch (Op0I->getOpcode()) { | 
|  | 3188 | case Instruction::Xor: | 
|  | 3189 | case Instruction::Or: | 
| Chris Lattner | 9e2c7fa | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 3190 | // If the mask is only needed on one incoming arm, push it up. | 
|  | 3191 | if (Op0I->hasOneUse()) { | 
|  | 3192 | if (MaskedValueIsZero(Op0LHS, NotAndRHS)) { | 
|  | 3193 | // Not masking anything out for the LHS, move to RHS. | 
|  | 3194 | Instruction *NewRHS = BinaryOperator::createAnd(Op0RHS, AndRHS, | 
|  | 3195 | Op0RHS->getName()+".masked"); | 
|  | 3196 | InsertNewInstBefore(NewRHS, I); | 
|  | 3197 | return BinaryOperator::create( | 
|  | 3198 | cast<BinaryOperator>(Op0I)->getOpcode(), Op0LHS, NewRHS); | 
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3199 | } | 
| Chris Lattner | c3ebf40 | 2006-02-07 07:27:52 +0000 | [diff] [blame] | 3200 | if (!isa<Constant>(Op0RHS) && | 
| Chris Lattner | 9e2c7fa | 2005-01-23 20:26:55 +0000 | [diff] [blame] | 3201 | MaskedValueIsZero(Op0RHS, NotAndRHS)) { | 
|  | 3202 | // Not masking anything out for the RHS, move to LHS. | 
|  | 3203 | Instruction *NewLHS = BinaryOperator::createAnd(Op0LHS, AndRHS, | 
|  | 3204 | Op0LHS->getName()+".masked"); | 
|  | 3205 | InsertNewInstBefore(NewLHS, I); | 
|  | 3206 | return BinaryOperator::create( | 
|  | 3207 | cast<BinaryOperator>(Op0I)->getOpcode(), NewLHS, Op0RHS); | 
|  | 3208 | } | 
|  | 3209 | } | 
|  | 3210 |  | 
| Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3211 | break; | 
| Chris Lattner | af51757 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3212 | case Instruction::Add: | 
| Chris Lattner | b4b2530 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3213 | // ((A & N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == AndRHS. | 
|  | 3214 | // ((A | N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0 | 
|  | 3215 | // ((A ^ N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0 | 
|  | 3216 | if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, false, I)) | 
|  | 3217 | return BinaryOperator::createAnd(V, AndRHS); | 
|  | 3218 | if (Value *V = FoldLogicalPlusAnd(Op0RHS, Op0LHS, AndRHS, false, I)) | 
|  | 3219 | return BinaryOperator::createAnd(V, AndRHS);  // Add commutes | 
| Chris Lattner | af51757 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3220 | break; | 
|  | 3221 |  | 
|  | 3222 | case Instruction::Sub: | 
| Chris Lattner | b4b2530 | 2005-09-18 07:22:02 +0000 | [diff] [blame] | 3223 | // ((A & N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == AndRHS. | 
|  | 3224 | // ((A | N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0 | 
|  | 3225 | // ((A ^ N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0 | 
|  | 3226 | if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, true, I)) | 
|  | 3227 | return BinaryOperator::createAnd(V, AndRHS); | 
| Chris Lattner | af51757 | 2005-09-18 04:24:45 +0000 | [diff] [blame] | 3228 | break; | 
| Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3229 | } | 
|  | 3230 |  | 
| Chris Lattner | 16464b3 | 2003-07-23 19:25:52 +0000 | [diff] [blame] | 3231 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) | 
| Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3232 | if (Instruction *Res = OptAndOp(Op0I, Op0CI, AndRHS, I)) | 
| Chris Lattner | ba1cb38 | 2003-09-19 17:17:26 +0000 | [diff] [blame] | 3233 | return Res; | 
| Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3234 | } else if (CastInst *CI = dyn_cast<CastInst>(Op0)) { | 
| Chris Lattner | 2c14cf7 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3235 | // If this is an integer truncation or change from signed-to-unsigned, and | 
|  | 3236 | // if the source is an and/or with immediate, transform it.  This | 
|  | 3237 | // frequently occurs for bitfield accesses. | 
|  | 3238 | if (Instruction *CastOp = dyn_cast<Instruction>(CI->getOperand(0))) { | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3239 | if ((isa<TruncInst>(CI) || isa<BitCastInst>(CI)) && | 
| Chris Lattner | 2c14cf7 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3240 | CastOp->getNumOperands() == 2) | 
| Chris Lattner | ab2dc4d | 2006-02-08 07:34:50 +0000 | [diff] [blame] | 3241 | if (ConstantInt *AndCI = dyn_cast<ConstantInt>(CastOp->getOperand(1))) | 
| Chris Lattner | 2c14cf7 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3242 | if (CastOp->getOpcode() == Instruction::And) { | 
|  | 3243 | // Change: and (cast (and X, C1) to T), C2 | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3244 | // into  : and (cast X to T), trunc_or_bitcast(C1)&C2 | 
|  | 3245 | // This will fold the two constants together, which may allow | 
|  | 3246 | // other simplifications. | 
| Reid Spencer | bb65ebf | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 3247 | Instruction *NewCast = CastInst::createTruncOrBitCast( | 
|  | 3248 | CastOp->getOperand(0), I.getType(), | 
|  | 3249 | CastOp->getName()+".shrunk"); | 
| Chris Lattner | 2c14cf7 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3250 | NewCast = InsertNewInstBefore(NewCast, I); | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3251 | // trunc_or_bitcast(C1)&C2 | 
| Reid Spencer | bb65ebf | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 3252 | Constant *C3 = ConstantExpr::getTruncOrBitCast(AndCI,I.getType()); | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3253 | C3 = ConstantExpr::getAnd(C3, AndRHS); | 
| Chris Lattner | 2c14cf7 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3254 | return BinaryOperator::createAnd(NewCast, C3); | 
|  | 3255 | } else if (CastOp->getOpcode() == Instruction::Or) { | 
|  | 3256 | // Change: and (cast (or X, C1) to T), C2 | 
|  | 3257 | // into  : trunc(C1)&C2 iff trunc(C1)&C2 == C2 | 
| Chris Lattner | 2dc148e | 2006-12-12 19:11:20 +0000 | [diff] [blame] | 3258 | Constant *C3 = ConstantExpr::getTruncOrBitCast(AndCI,I.getType()); | 
| Chris Lattner | 2c14cf7 | 2005-08-07 07:03:10 +0000 | [diff] [blame] | 3259 | if (ConstantExpr::getAnd(C3, AndRHS) == AndRHS)   // trunc(C1)&C2 | 
|  | 3260 | return ReplaceInstUsesWith(I, AndRHS); | 
|  | 3261 | } | 
|  | 3262 | } | 
| Chris Lattner | 33217db | 2003-07-23 19:36:21 +0000 | [diff] [blame] | 3263 | } | 
| Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3264 |  | 
|  | 3265 | // Try to fold constant and into select arguments. | 
|  | 3266 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) | 
| Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3267 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) | 
| Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3268 | return R; | 
| Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 3269 | if (isa<PHINode>(Op0)) | 
|  | 3270 | if (Instruction *NV = FoldOpIntoPhi(I)) | 
|  | 3271 | return NV; | 
| Chris Lattner | 49b47ae | 2003-07-23 17:57:01 +0000 | [diff] [blame] | 3272 | } | 
|  | 3273 |  | 
| Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 3274 | Value *Op0NotVal = dyn_castNotVal(Op0); | 
|  | 3275 | Value *Op1NotVal = dyn_castNotVal(Op1); | 
| Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3276 |  | 
| Chris Lattner | 023a483 | 2004-06-18 06:07:51 +0000 | [diff] [blame] | 3277 | if (Op0NotVal == Op1 || Op1NotVal == Op0)  // A & ~A  == ~A & A == 0 | 
|  | 3278 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); | 
|  | 3279 |  | 
| Misha Brukman | 9c003d8 | 2004-07-30 12:50:08 +0000 | [diff] [blame] | 3280 | // (~A & ~B) == (~(A | B)) - De Morgan's Law | 
| Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 3281 | if (Op0NotVal && Op1NotVal && isOnlyUse(Op0) && isOnlyUse(Op1)) { | 
| Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3282 | Instruction *Or = BinaryOperator::createOr(Op0NotVal, Op1NotVal, | 
|  | 3283 | I.getName()+".demorgan"); | 
| Chris Lattner | 49b47ae | 2003-07-23 17:57:01 +0000 | [diff] [blame] | 3284 | InsertNewInstBefore(Or, I); | 
| Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3285 | return BinaryOperator::createNot(Or); | 
|  | 3286 | } | 
| Chris Lattner | 8b10ab3 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 3287 |  | 
|  | 3288 | { | 
|  | 3289 | Value *A = 0, *B = 0; | 
| Chris Lattner | 8b10ab3 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 3290 | if (match(Op0, m_Or(m_Value(A), m_Value(B)))) | 
|  | 3291 | if (A == Op1 || B == Op1)    // (A | ?) & A  --> A | 
|  | 3292 | return ReplaceInstUsesWith(I, Op1); | 
|  | 3293 | if (match(Op1, m_Or(m_Value(A), m_Value(B)))) | 
|  | 3294 | if (A == Op0 || B == Op0)    // A & (A | ?)  --> A | 
|  | 3295 | return ReplaceInstUsesWith(I, Op0); | 
| Chris Lattner | dcd0792 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 3296 |  | 
|  | 3297 | if (Op0->hasOneUse() && | 
|  | 3298 | match(Op0, m_Xor(m_Value(A), m_Value(B)))) { | 
|  | 3299 | if (A == Op1) {                                // (A^B)&A -> A&(A^B) | 
|  | 3300 | I.swapOperands();     // Simplify below | 
|  | 3301 | std::swap(Op0, Op1); | 
|  | 3302 | } else if (B == Op1) {                         // (A^B)&B -> B&(B^A) | 
|  | 3303 | cast<BinaryOperator>(Op0)->swapOperands(); | 
|  | 3304 | I.swapOperands();     // Simplify below | 
|  | 3305 | std::swap(Op0, Op1); | 
|  | 3306 | } | 
|  | 3307 | } | 
|  | 3308 | if (Op1->hasOneUse() && | 
|  | 3309 | match(Op1, m_Xor(m_Value(A), m_Value(B)))) { | 
|  | 3310 | if (B == Op0) {                                // B&(A^B) -> B&(B^A) | 
|  | 3311 | cast<BinaryOperator>(Op1)->swapOperands(); | 
|  | 3312 | std::swap(A, B); | 
|  | 3313 | } | 
|  | 3314 | if (A == Op0) {                                // A&(A^B) -> A & ~B | 
|  | 3315 | Instruction *NotB = BinaryOperator::createNot(B, "tmp"); | 
|  | 3316 | InsertNewInstBefore(NotB, I); | 
|  | 3317 | return BinaryOperator::createAnd(A, NotB); | 
|  | 3318 | } | 
|  | 3319 | } | 
| Chris Lattner | 8b10ab3 | 2006-02-13 23:07:23 +0000 | [diff] [blame] | 3320 | } | 
|  | 3321 |  | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3322 | if (ICmpInst *RHS = dyn_cast<ICmpInst>(Op1)) { | 
|  | 3323 | // (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B) | 
|  | 3324 | if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS))) | 
| Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3325 | return R; | 
|  | 3326 |  | 
| Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3327 | Value *LHSVal, *RHSVal; | 
|  | 3328 | ConstantInt *LHSCst, *RHSCst; | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3329 | ICmpInst::Predicate LHSCC, RHSCC; | 
|  | 3330 | if (match(Op0, m_ICmp(LHSCC, m_Value(LHSVal), m_ConstantInt(LHSCst)))) | 
|  | 3331 | if (match(RHS, m_ICmp(RHSCC, m_Value(RHSVal), m_ConstantInt(RHSCst)))) | 
|  | 3332 | if (LHSVal == RHSVal &&    // Found (X icmp C1) & (X icmp C2) | 
|  | 3333 | // ICMP_[GL]E X, CST is folded to ICMP_[GL]T elsewhere. | 
|  | 3334 | LHSCC != ICmpInst::ICMP_UGE && LHSCC != ICmpInst::ICMP_ULE && | 
|  | 3335 | RHSCC != ICmpInst::ICMP_UGE && RHSCC != ICmpInst::ICMP_ULE && | 
|  | 3336 | LHSCC != ICmpInst::ICMP_SGE && LHSCC != ICmpInst::ICMP_SLE && | 
|  | 3337 | RHSCC != ICmpInst::ICMP_SGE && RHSCC != ICmpInst::ICMP_SLE) { | 
| Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3338 | // Ensure that the larger constant is on the RHS. | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3339 | ICmpInst::Predicate GT = ICmpInst::isSignedPredicate(LHSCC) ? | 
|  | 3340 | ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT; | 
|  | 3341 | Constant *Cmp = ConstantExpr::getICmp(GT, LHSCst, RHSCst); | 
|  | 3342 | ICmpInst *LHS = cast<ICmpInst>(Op0); | 
| Reid Spencer | cddc9df | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 3343 | if (cast<ConstantInt>(Cmp)->getZExtValue()) { | 
| Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3344 | std::swap(LHS, RHS); | 
|  | 3345 | std::swap(LHSCst, RHSCst); | 
|  | 3346 | std::swap(LHSCC, RHSCC); | 
|  | 3347 | } | 
|  | 3348 |  | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3349 | // At this point, we know we have have two icmp instructions | 
| Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3350 | // comparing a value against two constants and and'ing the result | 
|  | 3351 | // together.  Because of the above check, we know that we only have | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3352 | // icmp eq, icmp ne, icmp [su]lt, and icmp [SU]gt here. We also know | 
|  | 3353 | // (from the FoldICmpLogical check above), that the two constants | 
|  | 3354 | // are not equal and that the larger constant is on the RHS | 
| Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3355 | assert(LHSCst != RHSCst && "Compares not folded above?"); | 
|  | 3356 |  | 
|  | 3357 | switch (LHSCC) { | 
|  | 3358 | default: assert(0 && "Unknown integer condition code!"); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3359 | case ICmpInst::ICMP_EQ: | 
| Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3360 | switch (RHSCC) { | 
|  | 3361 | default: assert(0 && "Unknown integer condition code!"); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3362 | case ICmpInst::ICMP_EQ:         // (X == 13 & X == 15) -> false | 
|  | 3363 | case ICmpInst::ICMP_UGT:        // (X == 13 & X >  15) -> false | 
|  | 3364 | case ICmpInst::ICMP_SGT:        // (X == 13 & X >  15) -> false | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3365 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3366 | case ICmpInst::ICMP_NE:         // (X == 13 & X != 15) -> X == 13 | 
|  | 3367 | case ICmpInst::ICMP_ULT:        // (X == 13 & X <  15) -> X == 13 | 
|  | 3368 | case ICmpInst::ICMP_SLT:        // (X == 13 & X <  15) -> X == 13 | 
| Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3369 | return ReplaceInstUsesWith(I, LHS); | 
|  | 3370 | } | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3371 | case ICmpInst::ICMP_NE: | 
| Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3372 | switch (RHSCC) { | 
|  | 3373 | default: assert(0 && "Unknown integer condition code!"); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3374 | case ICmpInst::ICMP_ULT: | 
|  | 3375 | if (LHSCst == SubOne(RHSCst)) // (X != 13 & X u< 14) -> X < 13 | 
|  | 3376 | return new ICmpInst(ICmpInst::ICMP_ULT, LHSVal, LHSCst); | 
|  | 3377 | break;                        // (X != 13 & X u< 15) -> no change | 
|  | 3378 | case ICmpInst::ICMP_SLT: | 
|  | 3379 | if (LHSCst == SubOne(RHSCst)) // (X != 13 & X s< 14) -> X < 13 | 
|  | 3380 | return new ICmpInst(ICmpInst::ICMP_SLT, LHSVal, LHSCst); | 
|  | 3381 | break;                        // (X != 13 & X s< 15) -> no change | 
|  | 3382 | case ICmpInst::ICMP_EQ:         // (X != 13 & X == 15) -> X == 15 | 
|  | 3383 | case ICmpInst::ICMP_UGT:        // (X != 13 & X u> 15) -> X u> 15 | 
|  | 3384 | case ICmpInst::ICMP_SGT:        // (X != 13 & X s> 15) -> X s> 15 | 
| Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3385 | return ReplaceInstUsesWith(I, RHS); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3386 | case ICmpInst::ICMP_NE: | 
|  | 3387 | if (LHSCst == SubOne(RHSCst)){// (X != 13 & X != 14) -> X-13 >u 1 | 
| Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3388 | Constant *AddCST = ConstantExpr::getNeg(LHSCst); | 
|  | 3389 | Instruction *Add = BinaryOperator::createAdd(LHSVal, AddCST, | 
|  | 3390 | LHSVal->getName()+".off"); | 
|  | 3391 | InsertNewInstBefore(Add, I); | 
| Chris Lattner | c8fb6de | 2007-01-27 23:08:34 +0000 | [diff] [blame] | 3392 | return new ICmpInst(ICmpInst::ICMP_UGT, Add, | 
|  | 3393 | ConstantInt::get(Add->getType(), 1)); | 
| Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3394 | } | 
|  | 3395 | break;                        // (X != 13 & X != 15) -> no change | 
|  | 3396 | } | 
|  | 3397 | break; | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3398 | case ICmpInst::ICMP_ULT: | 
| Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3399 | switch (RHSCC) { | 
|  | 3400 | default: assert(0 && "Unknown integer condition code!"); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3401 | case ICmpInst::ICMP_EQ:         // (X u< 13 & X == 15) -> false | 
|  | 3402 | case ICmpInst::ICMP_UGT:        // (X u< 13 & X u> 15) -> false | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3403 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3404 | case ICmpInst::ICMP_SGT:        // (X u< 13 & X s> 15) -> no change | 
|  | 3405 | break; | 
|  | 3406 | case ICmpInst::ICMP_NE:         // (X u< 13 & X != 15) -> X u< 13 | 
|  | 3407 | case ICmpInst::ICMP_ULT:        // (X u< 13 & X u< 15) -> X u< 13 | 
| Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3408 | return ReplaceInstUsesWith(I, LHS); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3409 | case ICmpInst::ICMP_SLT:        // (X u< 13 & X s< 15) -> no change | 
|  | 3410 | break; | 
| Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3411 | } | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3412 | break; | 
|  | 3413 | case ICmpInst::ICMP_SLT: | 
| Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3414 | switch (RHSCC) { | 
|  | 3415 | default: assert(0 && "Unknown integer condition code!"); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3416 | case ICmpInst::ICMP_EQ:         // (X s< 13 & X == 15) -> false | 
|  | 3417 | case ICmpInst::ICMP_SGT:        // (X s< 13 & X s> 15) -> false | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3418 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3419 | case ICmpInst::ICMP_UGT:        // (X s< 13 & X u> 15) -> no change | 
|  | 3420 | break; | 
|  | 3421 | case ICmpInst::ICMP_NE:         // (X s< 13 & X != 15) -> X < 13 | 
|  | 3422 | case ICmpInst::ICMP_SLT:        // (X s< 13 & X s< 15) -> X < 13 | 
| Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3423 | return ReplaceInstUsesWith(I, LHS); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3424 | case ICmpInst::ICMP_ULT:        // (X s< 13 & X u< 15) -> no change | 
|  | 3425 | break; | 
| Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3426 | } | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3427 | break; | 
|  | 3428 | case ICmpInst::ICMP_UGT: | 
|  | 3429 | switch (RHSCC) { | 
|  | 3430 | default: assert(0 && "Unknown integer condition code!"); | 
|  | 3431 | case ICmpInst::ICMP_EQ:         // (X u> 13 & X == 15) -> X > 13 | 
|  | 3432 | return ReplaceInstUsesWith(I, LHS); | 
|  | 3433 | case ICmpInst::ICMP_UGT:        // (X u> 13 & X u> 15) -> X u> 15 | 
|  | 3434 | return ReplaceInstUsesWith(I, RHS); | 
|  | 3435 | case ICmpInst::ICMP_SGT:        // (X u> 13 & X s> 15) -> no change | 
|  | 3436 | break; | 
|  | 3437 | case ICmpInst::ICMP_NE: | 
|  | 3438 | if (RHSCst == AddOne(LHSCst)) // (X u> 13 & X != 14) -> X u> 14 | 
|  | 3439 | return new ICmpInst(LHSCC, LHSVal, RHSCst); | 
|  | 3440 | break;                        // (X u> 13 & X != 15) -> no change | 
|  | 3441 | case ICmpInst::ICMP_ULT:        // (X u> 13 & X u< 15) ->(X-14) <u 1 | 
|  | 3442 | return InsertRangeTest(LHSVal, AddOne(LHSCst), RHSCst, false, | 
|  | 3443 | true, I); | 
|  | 3444 | case ICmpInst::ICMP_SLT:        // (X u> 13 & X s< 15) -> no change | 
|  | 3445 | break; | 
|  | 3446 | } | 
|  | 3447 | break; | 
|  | 3448 | case ICmpInst::ICMP_SGT: | 
|  | 3449 | switch (RHSCC) { | 
|  | 3450 | default: assert(0 && "Unknown integer condition code!"); | 
|  | 3451 | case ICmpInst::ICMP_EQ:         // (X s> 13 & X == 15) -> X s> 13 | 
|  | 3452 | return ReplaceInstUsesWith(I, LHS); | 
|  | 3453 | case ICmpInst::ICMP_SGT:        // (X s> 13 & X s> 15) -> X s> 15 | 
|  | 3454 | return ReplaceInstUsesWith(I, RHS); | 
|  | 3455 | case ICmpInst::ICMP_UGT:        // (X s> 13 & X u> 15) -> no change | 
|  | 3456 | break; | 
|  | 3457 | case ICmpInst::ICMP_NE: | 
|  | 3458 | if (RHSCst == AddOne(LHSCst)) // (X s> 13 & X != 14) -> X s> 14 | 
|  | 3459 | return new ICmpInst(LHSCC, LHSVal, RHSCst); | 
|  | 3460 | break;                        // (X s> 13 & X != 15) -> no change | 
|  | 3461 | case ICmpInst::ICMP_SLT:        // (X s> 13 & X s< 15) ->(X-14) s< 1 | 
|  | 3462 | return InsertRangeTest(LHSVal, AddOne(LHSCst), RHSCst, true, | 
|  | 3463 | true, I); | 
|  | 3464 | case ICmpInst::ICMP_ULT:        // (X s> 13 & X u< 15) -> no change | 
|  | 3465 | break; | 
|  | 3466 | } | 
|  | 3467 | break; | 
| Chris Lattner | 623826c | 2004-09-28 21:48:02 +0000 | [diff] [blame] | 3468 | } | 
|  | 3469 | } | 
|  | 3470 | } | 
|  | 3471 |  | 
| Chris Lattner | 3af1053 | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3472 | // fold (and (cast A), (cast B)) -> (cast (and A, B)) | 
| Reid Spencer | 799b5bf | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 3473 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) | 
|  | 3474 | if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) | 
|  | 3475 | if (Op0C->getOpcode() == Op1C->getOpcode()) { // same cast kind ? | 
|  | 3476 | const Type *SrcTy = Op0C->getOperand(0)->getType(); | 
| Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 3477 | if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() && | 
| Reid Spencer | 799b5bf | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 3478 | // Only do this if the casts both really cause code to be generated. | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3479 | ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0), | 
|  | 3480 | I.getType(), TD) && | 
|  | 3481 | ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0), | 
|  | 3482 | I.getType(), TD)) { | 
| Reid Spencer | 799b5bf | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 3483 | Instruction *NewOp = BinaryOperator::createAnd(Op0C->getOperand(0), | 
|  | 3484 | Op1C->getOperand(0), | 
|  | 3485 | I.getName()); | 
|  | 3486 | InsertNewInstBefore(NewOp, I); | 
|  | 3487 | return CastInst::create(Op0C->getOpcode(), NewOp, I.getType()); | 
|  | 3488 | } | 
| Chris Lattner | 3af1053 | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3489 | } | 
| Chris Lattner | f05d69a | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 3490 |  | 
|  | 3491 | // (X >> Z) & (Y >> Z)  -> (X&Y) >> Z  for all shifts. | 
| Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3492 | if (BinaryOperator *SI1 = dyn_cast<BinaryOperator>(Op1)) { | 
|  | 3493 | if (BinaryOperator *SI0 = dyn_cast<BinaryOperator>(Op0)) | 
|  | 3494 | if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() && | 
| Chris Lattner | f05d69a | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 3495 | SI0->getOperand(1) == SI1->getOperand(1) && | 
|  | 3496 | (SI0->hasOneUse() || SI1->hasOneUse())) { | 
|  | 3497 | Instruction *NewOp = | 
|  | 3498 | InsertNewInstBefore(BinaryOperator::createAnd(SI0->getOperand(0), | 
|  | 3499 | SI1->getOperand(0), | 
|  | 3500 | SI0->getName()), I); | 
| Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3501 | return BinaryOperator::create(SI1->getOpcode(), NewOp, | 
|  | 3502 | SI1->getOperand(1)); | 
| Chris Lattner | f05d69a | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 3503 | } | 
| Chris Lattner | 3af1053 | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3504 | } | 
|  | 3505 |  | 
| Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3506 | return Changed ? &I : 0; | 
| Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3507 | } | 
|  | 3508 |  | 
| Chris Lattner | c482a9e | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3509 | /// CollectBSwapParts - Look to see if the specified value defines a single byte | 
|  | 3510 | /// in the result.  If it does, and if the specified byte hasn't been filled in | 
|  | 3511 | /// yet, fill it in and return false. | 
| Chris Lattner | 99c6cf6 | 2007-02-15 22:52:10 +0000 | [diff] [blame] | 3512 | static bool CollectBSwapParts(Value *V, SmallVector<Value*, 8> &ByteValues) { | 
| Chris Lattner | c482a9e | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3513 | Instruction *I = dyn_cast<Instruction>(V); | 
|  | 3514 | if (I == 0) return true; | 
|  | 3515 |  | 
|  | 3516 | // If this is an or instruction, it is an inner node of the bswap. | 
|  | 3517 | if (I->getOpcode() == Instruction::Or) | 
|  | 3518 | return CollectBSwapParts(I->getOperand(0), ByteValues) || | 
|  | 3519 | CollectBSwapParts(I->getOperand(1), ByteValues); | 
|  | 3520 |  | 
| Zhou Sheng | b25806f | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3521 | uint32_t BitWidth = I->getType()->getPrimitiveSizeInBits(); | 
| Chris Lattner | c482a9e | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3522 | // If this is a shift by a constant int, and it is "24", then its operand | 
|  | 3523 | // defines a byte.  We only handle unsigned types here. | 
| Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3524 | if (I->isShift() && isa<ConstantInt>(I->getOperand(1))) { | 
| Chris Lattner | c482a9e | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3525 | // Not shifting the entire input by N-1 bytes? | 
| Zhou Sheng | b25806f | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3526 | if (cast<ConstantInt>(I->getOperand(1))->getLimitedValue(BitWidth) != | 
| Chris Lattner | c482a9e | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3527 | 8*(ByteValues.size()-1)) | 
|  | 3528 | return true; | 
|  | 3529 |  | 
|  | 3530 | unsigned DestNo; | 
|  | 3531 | if (I->getOpcode() == Instruction::Shl) { | 
|  | 3532 | // X << 24 defines the top byte with the lowest of the input bytes. | 
|  | 3533 | DestNo = ByteValues.size()-1; | 
|  | 3534 | } else { | 
|  | 3535 | // X >>u 24 defines the low byte with the highest of the input bytes. | 
|  | 3536 | DestNo = 0; | 
|  | 3537 | } | 
|  | 3538 |  | 
|  | 3539 | // If the destination byte value is already defined, the values are or'd | 
|  | 3540 | // together, which isn't a bswap (unless it's an or of the same bits). | 
|  | 3541 | if (ByteValues[DestNo] && ByteValues[DestNo] != I->getOperand(0)) | 
|  | 3542 | return true; | 
|  | 3543 | ByteValues[DestNo] = I->getOperand(0); | 
|  | 3544 | return false; | 
|  | 3545 | } | 
|  | 3546 |  | 
|  | 3547 | // Otherwise, we can only handle and(shift X, imm), imm).  Bail out of if we | 
|  | 3548 | // don't have this. | 
|  | 3549 | Value *Shift = 0, *ShiftLHS = 0; | 
|  | 3550 | ConstantInt *AndAmt = 0, *ShiftAmt = 0; | 
|  | 3551 | if (!match(I, m_And(m_Value(Shift), m_ConstantInt(AndAmt))) || | 
|  | 3552 | !match(Shift, m_Shift(m_Value(ShiftLHS), m_ConstantInt(ShiftAmt)))) | 
|  | 3553 | return true; | 
|  | 3554 | Instruction *SI = cast<Instruction>(Shift); | 
|  | 3555 |  | 
|  | 3556 | // Make sure that the shift amount is by a multiple of 8 and isn't too big. | 
| Zhou Sheng | b25806f | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3557 | if (ShiftAmt->getLimitedValue(BitWidth) & 7 || | 
|  | 3558 | ShiftAmt->getLimitedValue(BitWidth) > 8*ByteValues.size()) | 
| Chris Lattner | c482a9e | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3559 | return true; | 
|  | 3560 |  | 
|  | 3561 | // Turn 0xFF -> 0, 0xFF00 -> 1, 0xFF0000 -> 2, etc. | 
|  | 3562 | unsigned DestByte; | 
| Zhou Sheng | b25806f | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3563 | if (AndAmt->getValue().getActiveBits() > 64) | 
|  | 3564 | return true; | 
|  | 3565 | uint64_t AndAmtVal = AndAmt->getZExtValue(); | 
| Chris Lattner | c482a9e | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3566 | for (DestByte = 0; DestByte != ByteValues.size(); ++DestByte) | 
| Zhou Sheng | b25806f | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 3567 | if (AndAmtVal == uint64_t(0xFF) << 8*DestByte) | 
| Chris Lattner | c482a9e | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3568 | break; | 
|  | 3569 | // Unknown mask for bswap. | 
|  | 3570 | if (DestByte == ByteValues.size()) return true; | 
|  | 3571 |  | 
| Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3572 | unsigned ShiftBytes = ShiftAmt->getZExtValue()/8; | 
| Chris Lattner | c482a9e | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3573 | unsigned SrcByte; | 
|  | 3574 | if (SI->getOpcode() == Instruction::Shl) | 
|  | 3575 | SrcByte = DestByte - ShiftBytes; | 
|  | 3576 | else | 
|  | 3577 | SrcByte = DestByte + ShiftBytes; | 
|  | 3578 |  | 
|  | 3579 | // If the SrcByte isn't a bswapped value from the DestByte, reject it. | 
|  | 3580 | if (SrcByte != ByteValues.size()-DestByte-1) | 
|  | 3581 | return true; | 
|  | 3582 |  | 
|  | 3583 | // If the destination byte value is already defined, the values are or'd | 
|  | 3584 | // together, which isn't a bswap (unless it's an or of the same bits). | 
|  | 3585 | if (ByteValues[DestByte] && ByteValues[DestByte] != SI->getOperand(0)) | 
|  | 3586 | return true; | 
|  | 3587 | ByteValues[DestByte] = SI->getOperand(0); | 
|  | 3588 | return false; | 
|  | 3589 | } | 
|  | 3590 |  | 
|  | 3591 | /// MatchBSwap - Given an OR instruction, check to see if this is a bswap idiom. | 
|  | 3592 | /// If so, insert the new bswap intrinsic and return it. | 
|  | 3593 | Instruction *InstCombiner::MatchBSwap(BinaryOperator &I) { | 
| Chris Lattner | c3eeb42 | 2007-04-01 20:57:36 +0000 | [diff] [blame] | 3594 | const IntegerType *ITy = dyn_cast<IntegerType>(I.getType()); | 
|  | 3595 | if (!ITy || ITy->getBitWidth() % 16) | 
|  | 3596 | return 0;   // Can only bswap pairs of bytes.  Can't do vectors. | 
| Chris Lattner | c482a9e | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3597 |  | 
|  | 3598 | /// ByteValues - For each byte of the result, we keep track of which value | 
|  | 3599 | /// defines each byte. | 
| Chris Lattner | 99c6cf6 | 2007-02-15 22:52:10 +0000 | [diff] [blame] | 3600 | SmallVector<Value*, 8> ByteValues; | 
| Chris Lattner | c3eeb42 | 2007-04-01 20:57:36 +0000 | [diff] [blame] | 3601 | ByteValues.resize(ITy->getBitWidth()/8); | 
| Chris Lattner | c482a9e | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3602 |  | 
|  | 3603 | // Try to find all the pieces corresponding to the bswap. | 
|  | 3604 | if (CollectBSwapParts(I.getOperand(0), ByteValues) || | 
|  | 3605 | CollectBSwapParts(I.getOperand(1), ByteValues)) | 
|  | 3606 | return 0; | 
|  | 3607 |  | 
|  | 3608 | // Check to see if all of the bytes come from the same value. | 
|  | 3609 | Value *V = ByteValues[0]; | 
|  | 3610 | if (V == 0) return 0;  // Didn't find a byte?  Must be zero. | 
|  | 3611 |  | 
|  | 3612 | // Check to make sure that all of the bytes come from the same value. | 
|  | 3613 | for (unsigned i = 1, e = ByteValues.size(); i != e; ++i) | 
|  | 3614 | if (ByteValues[i] != V) | 
|  | 3615 | return 0; | 
| Chris Lattner | c3eeb42 | 2007-04-01 20:57:36 +0000 | [diff] [blame] | 3616 | const Type *Tys[] = { ITy, ITy }; | 
| Chris Lattner | c482a9e | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3617 | Module *M = I.getParent()->getParent()->getParent(); | 
| Chris Lattner | c3eeb42 | 2007-04-01 20:57:36 +0000 | [diff] [blame] | 3618 | Function *F = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 2); | 
| Chris Lattner | c482a9e | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3619 | return new CallInst(F, V); | 
|  | 3620 | } | 
|  | 3621 |  | 
|  | 3622 |  | 
| Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3623 | Instruction *InstCombiner::visitOr(BinaryOperator &I) { | 
| Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 3624 | bool Changed = SimplifyCommutative(I); | 
| Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3625 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); | 
| Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3626 |  | 
| Chris Lattner | 3a8248f | 2007-03-24 23:56:43 +0000 | [diff] [blame] | 3627 | if (isa<UndefValue>(Op1))                       // X | undef -> -1 | 
|  | 3628 | return ReplaceInstUsesWith(I, ConstantInt::getAllOnesValue(I.getType())); | 
| Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3629 |  | 
| Chris Lattner | 5b2edb1 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3630 | // or X, X = X | 
|  | 3631 | if (Op0 == Op1) | 
| Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 3632 | return ReplaceInstUsesWith(I, Op0); | 
| Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3633 |  | 
| Chris Lattner | 5b2edb1 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3634 | // See if we can simplify any instructions used by the instruction whose sole | 
|  | 3635 | // purpose is to compute bits we don't care about. | 
| Chris Lattner | 3a8248f | 2007-03-24 23:56:43 +0000 | [diff] [blame] | 3636 | if (!isa<VectorType>(I.getType())) { | 
|  | 3637 | uint32_t BitWidth = cast<IntegerType>(I.getType())->getBitWidth(); | 
|  | 3638 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); | 
|  | 3639 | if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth), | 
|  | 3640 | KnownZero, KnownOne)) | 
|  | 3641 | return &I; | 
|  | 3642 | } | 
| Chris Lattner | 5b2edb1 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3643 |  | 
| Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3644 | // or X, -1 == -1 | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3645 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { | 
| Chris Lattner | 330628a | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 3646 | ConstantInt *C1 = 0; Value *X = 0; | 
| Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3647 | // (X & C1) | C2 --> (X | C2) & (C1|C2) | 
|  | 3648 | if (match(Op0, m_And(m_Value(X), m_ConstantInt(C1))) && isOnlyUse(Op0)) { | 
| Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3649 | Instruction *Or = BinaryOperator::createOr(X, RHS); | 
| Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3650 | InsertNewInstBefore(Or, I); | 
| Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3651 | Or->takeName(Op0); | 
| Zhou Sheng | 9bc8ab1 | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 3652 | return BinaryOperator::createAnd(Or, | 
|  | 3653 | ConstantInt::get(RHS->getValue() | C1->getValue())); | 
| Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3654 | } | 
| Chris Lattner | 8f0d156 | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 3655 |  | 
| Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3656 | // (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2) | 
|  | 3657 | if (match(Op0, m_Xor(m_Value(X), m_ConstantInt(C1))) && isOnlyUse(Op0)) { | 
| Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3658 | Instruction *Or = BinaryOperator::createOr(X, RHS); | 
| Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3659 | InsertNewInstBefore(Or, I); | 
| Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3660 | Or->takeName(Op0); | 
| Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3661 | return BinaryOperator::createXor(Or, | 
| Zhou Sheng | 9bc8ab1 | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 3662 | ConstantInt::get(C1->getValue() & ~RHS->getValue())); | 
| Chris Lattner | 8f0d156 | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 3663 | } | 
| Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3664 |  | 
|  | 3665 | // Try to fold constant and into select arguments. | 
|  | 3666 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) | 
| Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 3667 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) | 
| Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 3668 | return R; | 
| Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 3669 | if (isa<PHINode>(Op0)) | 
|  | 3670 | if (Instruction *NV = FoldOpIntoPhi(I)) | 
|  | 3671 | return NV; | 
| Chris Lattner | 8f0d156 | 2003-07-23 18:29:44 +0000 | [diff] [blame] | 3672 | } | 
|  | 3673 |  | 
| Chris Lattner | 330628a | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 3674 | Value *A = 0, *B = 0; | 
|  | 3675 | ConstantInt *C1 = 0, *C2 = 0; | 
| Chris Lattner | 4294cec | 2005-05-07 23:49:08 +0000 | [diff] [blame] | 3676 |  | 
|  | 3677 | if (match(Op0, m_And(m_Value(A), m_Value(B)))) | 
|  | 3678 | if (A == Op1 || B == Op1)    // (A & ?) | A  --> A | 
|  | 3679 | return ReplaceInstUsesWith(I, Op1); | 
|  | 3680 | if (match(Op1, m_And(m_Value(A), m_Value(B)))) | 
|  | 3681 | if (A == Op0 || B == Op0)    // A | (A & ?)  --> A | 
|  | 3682 | return ReplaceInstUsesWith(I, Op0); | 
|  | 3683 |  | 
| Chris Lattner | b7845d6 | 2006-07-10 20:25:24 +0000 | [diff] [blame] | 3684 | // (A | B) | C  and  A | (B | C)                  -> bswap if possible. | 
|  | 3685 | // (A >> B) | (C << D)  and  (A << B) | (B >> C)  -> bswap if possible. | 
| Chris Lattner | c482a9e | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3686 | if (match(Op0, m_Or(m_Value(), m_Value())) || | 
| Chris Lattner | b7845d6 | 2006-07-10 20:25:24 +0000 | [diff] [blame] | 3687 | match(Op1, m_Or(m_Value(), m_Value())) || | 
|  | 3688 | (match(Op0, m_Shift(m_Value(), m_Value())) && | 
|  | 3689 | match(Op1, m_Shift(m_Value(), m_Value())))) { | 
| Chris Lattner | c482a9e | 2006-06-15 19:07:26 +0000 | [diff] [blame] | 3690 | if (Instruction *BSwap = MatchBSwap(I)) | 
|  | 3691 | return BSwap; | 
|  | 3692 | } | 
|  | 3693 |  | 
| Chris Lattner | b62f508 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 3694 | // (X^C)|Y -> (X|Y)^C iff Y&C == 0 | 
|  | 3695 | if (Op0->hasOneUse() && match(Op0, m_Xor(m_Value(A), m_ConstantInt(C1))) && | 
| Reid Spencer | b722f2b | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 3696 | MaskedValueIsZero(Op1, C1->getValue())) { | 
| Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3697 | Instruction *NOr = BinaryOperator::createOr(A, Op1); | 
|  | 3698 | InsertNewInstBefore(NOr, I); | 
|  | 3699 | NOr->takeName(Op0); | 
|  | 3700 | return BinaryOperator::createXor(NOr, C1); | 
| Chris Lattner | b62f508 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 3701 | } | 
|  | 3702 |  | 
|  | 3703 | // Y|(X^C) -> (X|Y)^C iff Y&C == 0 | 
|  | 3704 | if (Op1->hasOneUse() && match(Op1, m_Xor(m_Value(A), m_ConstantInt(C1))) && | 
| Reid Spencer | b722f2b | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 3705 | MaskedValueIsZero(Op0, C1->getValue())) { | 
| Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 3706 | Instruction *NOr = BinaryOperator::createOr(A, Op0); | 
|  | 3707 | InsertNewInstBefore(NOr, I); | 
|  | 3708 | NOr->takeName(Op0); | 
|  | 3709 | return BinaryOperator::createXor(NOr, C1); | 
| Chris Lattner | b62f508 | 2005-05-09 04:58:36 +0000 | [diff] [blame] | 3710 | } | 
|  | 3711 |  | 
| Chris Lattner | 1521298 | 2005-09-18 03:42:07 +0000 | [diff] [blame] | 3712 | // (A & C1)|(B & C2) | 
| Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3713 | if (match(Op0, m_And(m_Value(A), m_ConstantInt(C1))) && | 
| Chris Lattner | 1521298 | 2005-09-18 03:42:07 +0000 | [diff] [blame] | 3714 | match(Op1, m_And(m_Value(B), m_ConstantInt(C2)))) { | 
|  | 3715 |  | 
|  | 3716 | if (A == B)  // (A & C1)|(A & C2) == A & (C1|C2) | 
| Zhou Sheng | 9bc8ab1 | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 3717 | return BinaryOperator::createAnd(A, | 
|  | 3718 | ConstantInt::get(C1->getValue() | C2->getValue())); | 
| Chris Lattner | 1521298 | 2005-09-18 03:42:07 +0000 | [diff] [blame] | 3719 |  | 
|  | 3720 |  | 
| Chris Lattner | 01f56c6 | 2005-09-18 06:02:59 +0000 | [diff] [blame] | 3721 | // If we have: ((V + N) & C1) | (V & C2) | 
|  | 3722 | // .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0 | 
|  | 3723 | // replace with V+N. | 
| Zhou Sheng | 9bc8ab1 | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 3724 | if (C1->getValue() == ~C2->getValue()) { | 
| Chris Lattner | 330628a | 2006-01-06 17:59:59 +0000 | [diff] [blame] | 3725 | Value *V1 = 0, *V2 = 0; | 
| Reid Spencer | b722f2b | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 3726 | if ((C2->getValue() & (C2->getValue()+1)) == 0 && // C2 == 0+1+ | 
| Chris Lattner | 01f56c6 | 2005-09-18 06:02:59 +0000 | [diff] [blame] | 3727 | match(A, m_Add(m_Value(V1), m_Value(V2)))) { | 
|  | 3728 | // Add commutes, try both ways. | 
| Reid Spencer | b722f2b | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 3729 | if (V1 == B && MaskedValueIsZero(V2, C2->getValue())) | 
| Chris Lattner | 01f56c6 | 2005-09-18 06:02:59 +0000 | [diff] [blame] | 3730 | return ReplaceInstUsesWith(I, A); | 
| Reid Spencer | b722f2b | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 3731 | if (V2 == B && MaskedValueIsZero(V1, C2->getValue())) | 
| Chris Lattner | 01f56c6 | 2005-09-18 06:02:59 +0000 | [diff] [blame] | 3732 | return ReplaceInstUsesWith(I, A); | 
|  | 3733 | } | 
|  | 3734 | // Or commutes, try both ways. | 
| Reid Spencer | b722f2b | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 3735 | if ((C1->getValue() & (C1->getValue()+1)) == 0 && | 
| Chris Lattner | 01f56c6 | 2005-09-18 06:02:59 +0000 | [diff] [blame] | 3736 | match(B, m_Add(m_Value(V1), m_Value(V2)))) { | 
|  | 3737 | // Add commutes, try both ways. | 
| Reid Spencer | b722f2b | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 3738 | if (V1 == A && MaskedValueIsZero(V2, C1->getValue())) | 
| Chris Lattner | 01f56c6 | 2005-09-18 06:02:59 +0000 | [diff] [blame] | 3739 | return ReplaceInstUsesWith(I, B); | 
| Reid Spencer | b722f2b | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 3740 | if (V2 == A && MaskedValueIsZero(V1, C1->getValue())) | 
| Chris Lattner | 01f56c6 | 2005-09-18 06:02:59 +0000 | [diff] [blame] | 3741 | return ReplaceInstUsesWith(I, B); | 
| Chris Lattner | 1521298 | 2005-09-18 03:42:07 +0000 | [diff] [blame] | 3742 | } | 
|  | 3743 | } | 
|  | 3744 | } | 
| Chris Lattner | f05d69a | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 3745 |  | 
|  | 3746 | // (X >> Z) | (Y >> Z)  -> (X|Y) >> Z  for all shifts. | 
| Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3747 | if (BinaryOperator *SI1 = dyn_cast<BinaryOperator>(Op1)) { | 
|  | 3748 | if (BinaryOperator *SI0 = dyn_cast<BinaryOperator>(Op0)) | 
|  | 3749 | if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() && | 
| Chris Lattner | f05d69a | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 3750 | SI0->getOperand(1) == SI1->getOperand(1) && | 
|  | 3751 | (SI0->hasOneUse() || SI1->hasOneUse())) { | 
|  | 3752 | Instruction *NewOp = | 
|  | 3753 | InsertNewInstBefore(BinaryOperator::createOr(SI0->getOperand(0), | 
|  | 3754 | SI1->getOperand(0), | 
|  | 3755 | SI0->getName()), I); | 
| Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3756 | return BinaryOperator::create(SI1->getOpcode(), NewOp, | 
|  | 3757 | SI1->getOperand(1)); | 
| Chris Lattner | f05d69a | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 3758 | } | 
|  | 3759 | } | 
| Chris Lattner | 812aab7 | 2003-08-12 19:11:07 +0000 | [diff] [blame] | 3760 |  | 
| Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3761 | if (match(Op0, m_Not(m_Value(A)))) {   // ~A | Op1 | 
|  | 3762 | if (A == Op1)   // ~A | A == -1 | 
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3763 | return ReplaceInstUsesWith(I, | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3764 | ConstantInt::getAllOnesValue(I.getType())); | 
| Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3765 | } else { | 
|  | 3766 | A = 0; | 
|  | 3767 | } | 
| Chris Lattner | 4294cec | 2005-05-07 23:49:08 +0000 | [diff] [blame] | 3768 | // Note, A is still live here! | 
| Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3769 | if (match(Op1, m_Not(m_Value(B)))) {   // Op0 | ~B | 
|  | 3770 | if (Op0 == B) | 
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3771 | return ReplaceInstUsesWith(I, | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3772 | ConstantInt::getAllOnesValue(I.getType())); | 
| Chris Lattner | 3e327a4 | 2003-03-10 23:13:59 +0000 | [diff] [blame] | 3773 |  | 
| Misha Brukman | 9c003d8 | 2004-07-30 12:50:08 +0000 | [diff] [blame] | 3774 | // (~A | ~B) == (~(A & B)) - De Morgan's Law | 
| Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 3775 | if (A && isOnlyUse(Op0) && isOnlyUse(Op1)) { | 
|  | 3776 | Value *And = InsertNewInstBefore(BinaryOperator::createAnd(A, B, | 
|  | 3777 | I.getName()+".demorgan"), I); | 
|  | 3778 | return BinaryOperator::createNot(And); | 
|  | 3779 | } | 
| Chris Lattner | 3e327a4 | 2003-03-10 23:13:59 +0000 | [diff] [blame] | 3780 | } | 
| Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 3781 |  | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3782 | // (icmp1 A, B) | (icmp2 A, B) --> (icmp3 A, B) | 
|  | 3783 | if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1))) { | 
|  | 3784 | if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS))) | 
| Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 3785 | return R; | 
|  | 3786 |  | 
| Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3787 | Value *LHSVal, *RHSVal; | 
|  | 3788 | ConstantInt *LHSCst, *RHSCst; | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3789 | ICmpInst::Predicate LHSCC, RHSCC; | 
|  | 3790 | if (match(Op0, m_ICmp(LHSCC, m_Value(LHSVal), m_ConstantInt(LHSCst)))) | 
|  | 3791 | if (match(RHS, m_ICmp(RHSCC, m_Value(RHSVal), m_ConstantInt(RHSCst)))) | 
|  | 3792 | if (LHSVal == RHSVal &&    // Found (X icmp C1) | (X icmp C2) | 
|  | 3793 | // icmp [us][gl]e x, cst is folded to icmp [us][gl]t elsewhere. | 
|  | 3794 | LHSCC != ICmpInst::ICMP_UGE && LHSCC != ICmpInst::ICMP_ULE && | 
|  | 3795 | RHSCC != ICmpInst::ICMP_UGE && RHSCC != ICmpInst::ICMP_ULE && | 
|  | 3796 | LHSCC != ICmpInst::ICMP_SGE && LHSCC != ICmpInst::ICMP_SLE && | 
|  | 3797 | RHSCC != ICmpInst::ICMP_SGE && RHSCC != ICmpInst::ICMP_SLE) { | 
| Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3798 | // Ensure that the larger constant is on the RHS. | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3799 | ICmpInst::Predicate GT = ICmpInst::isSignedPredicate(LHSCC) ? | 
|  | 3800 | ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT; | 
|  | 3801 | Constant *Cmp = ConstantExpr::getICmp(GT, LHSCst, RHSCst); | 
|  | 3802 | ICmpInst *LHS = cast<ICmpInst>(Op0); | 
| Reid Spencer | cddc9df | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 3803 | if (cast<ConstantInt>(Cmp)->getZExtValue()) { | 
| Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3804 | std::swap(LHS, RHS); | 
|  | 3805 | std::swap(LHSCst, RHSCst); | 
|  | 3806 | std::swap(LHSCC, RHSCC); | 
|  | 3807 | } | 
|  | 3808 |  | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3809 | // At this point, we know we have have two icmp instructions | 
| Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3810 | // comparing a value against two constants and or'ing the result | 
|  | 3811 | // together.  Because of the above check, we know that we only have | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3812 | // ICMP_EQ, ICMP_NE, ICMP_LT, and ICMP_GT here. We also know (from the | 
|  | 3813 | // FoldICmpLogical check above), that the two constants are not | 
| Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3814 | // equal. | 
|  | 3815 | assert(LHSCst != RHSCst && "Compares not folded above?"); | 
|  | 3816 |  | 
|  | 3817 | switch (LHSCC) { | 
|  | 3818 | default: assert(0 && "Unknown integer condition code!"); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3819 | case ICmpInst::ICMP_EQ: | 
| Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3820 | switch (RHSCC) { | 
|  | 3821 | default: assert(0 && "Unknown integer condition code!"); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3822 | case ICmpInst::ICMP_EQ: | 
| Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3823 | if (LHSCst == SubOne(RHSCst)) {// (X == 13 | X == 14) -> X-13 <u 2 | 
|  | 3824 | Constant *AddCST = ConstantExpr::getNeg(LHSCst); | 
|  | 3825 | Instruction *Add = BinaryOperator::createAdd(LHSVal, AddCST, | 
|  | 3826 | LHSVal->getName()+".off"); | 
|  | 3827 | InsertNewInstBefore(Add, I); | 
| Zhou Sheng | 9bc8ab1 | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 3828 | AddCST = Subtract(AddOne(RHSCst), LHSCst); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3829 | return new ICmpInst(ICmpInst::ICMP_ULT, Add, AddCST); | 
| Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3830 | } | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3831 | break;                         // (X == 13 | X == 15) -> no change | 
|  | 3832 | case ICmpInst::ICMP_UGT:         // (X == 13 | X u> 14) -> no change | 
|  | 3833 | case ICmpInst::ICMP_SGT:         // (X == 13 | X s> 14) -> no change | 
| Chris Lattner | 5c21946 | 2005-04-19 06:04:18 +0000 | [diff] [blame] | 3834 | break; | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3835 | case ICmpInst::ICMP_NE:          // (X == 13 | X != 15) -> X != 15 | 
|  | 3836 | case ICmpInst::ICMP_ULT:         // (X == 13 | X u< 15) -> X u< 15 | 
|  | 3837 | case ICmpInst::ICMP_SLT:         // (X == 13 | X s< 15) -> X s< 15 | 
| Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3838 | return ReplaceInstUsesWith(I, RHS); | 
|  | 3839 | } | 
|  | 3840 | break; | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3841 | case ICmpInst::ICMP_NE: | 
| Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3842 | switch (RHSCC) { | 
|  | 3843 | default: assert(0 && "Unknown integer condition code!"); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3844 | case ICmpInst::ICMP_EQ:          // (X != 13 | X == 15) -> X != 13 | 
|  | 3845 | case ICmpInst::ICMP_UGT:         // (X != 13 | X u> 15) -> X != 13 | 
|  | 3846 | case ICmpInst::ICMP_SGT:         // (X != 13 | X s> 15) -> X != 13 | 
| Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3847 | return ReplaceInstUsesWith(I, LHS); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3848 | case ICmpInst::ICMP_NE:          // (X != 13 | X != 15) -> true | 
|  | 3849 | case ICmpInst::ICMP_ULT:         // (X != 13 | X u< 15) -> true | 
|  | 3850 | case ICmpInst::ICMP_SLT:         // (X != 13 | X s< 15) -> true | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3851 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); | 
| Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3852 | } | 
|  | 3853 | break; | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3854 | case ICmpInst::ICMP_ULT: | 
| Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3855 | switch (RHSCC) { | 
|  | 3856 | default: assert(0 && "Unknown integer condition code!"); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3857 | case ICmpInst::ICMP_EQ:         // (X u< 13 | X == 14) -> no change | 
| Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3858 | break; | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3859 | case ICmpInst::ICMP_UGT:        // (X u< 13 | X u> 15) ->(X-13) u> 2 | 
|  | 3860 | return InsertRangeTest(LHSVal, LHSCst, AddOne(RHSCst), false, | 
|  | 3861 | false, I); | 
|  | 3862 | case ICmpInst::ICMP_SGT:        // (X u< 13 | X s> 15) -> no change | 
|  | 3863 | break; | 
|  | 3864 | case ICmpInst::ICMP_NE:         // (X u< 13 | X != 15) -> X != 15 | 
|  | 3865 | case ICmpInst::ICMP_ULT:        // (X u< 13 | X u< 15) -> X u< 15 | 
| Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3866 | return ReplaceInstUsesWith(I, RHS); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3867 | case ICmpInst::ICMP_SLT:        // (X u< 13 | X s< 15) -> no change | 
|  | 3868 | break; | 
| Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3869 | } | 
|  | 3870 | break; | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3871 | case ICmpInst::ICMP_SLT: | 
| Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3872 | switch (RHSCC) { | 
|  | 3873 | default: assert(0 && "Unknown integer condition code!"); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3874 | case ICmpInst::ICMP_EQ:         // (X s< 13 | X == 14) -> no change | 
|  | 3875 | break; | 
|  | 3876 | case ICmpInst::ICMP_SGT:        // (X s< 13 | X s> 15) ->(X-13) s> 2 | 
|  | 3877 | return InsertRangeTest(LHSVal, LHSCst, AddOne(RHSCst), true, | 
|  | 3878 | false, I); | 
|  | 3879 | case ICmpInst::ICMP_UGT:        // (X s< 13 | X u> 15) -> no change | 
|  | 3880 | break; | 
|  | 3881 | case ICmpInst::ICMP_NE:         // (X s< 13 | X != 15) -> X != 15 | 
|  | 3882 | case ICmpInst::ICMP_SLT:        // (X s< 13 | X s< 15) -> X s< 15 | 
|  | 3883 | return ReplaceInstUsesWith(I, RHS); | 
|  | 3884 | case ICmpInst::ICMP_ULT:        // (X s< 13 | X u< 15) -> no change | 
|  | 3885 | break; | 
| Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3886 | } | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3887 | break; | 
|  | 3888 | case ICmpInst::ICMP_UGT: | 
|  | 3889 | switch (RHSCC) { | 
|  | 3890 | default: assert(0 && "Unknown integer condition code!"); | 
|  | 3891 | case ICmpInst::ICMP_EQ:         // (X u> 13 | X == 15) -> X u> 13 | 
|  | 3892 | case ICmpInst::ICMP_UGT:        // (X u> 13 | X u> 15) -> X u> 13 | 
|  | 3893 | return ReplaceInstUsesWith(I, LHS); | 
|  | 3894 | case ICmpInst::ICMP_SGT:        // (X u> 13 | X s> 15) -> no change | 
|  | 3895 | break; | 
|  | 3896 | case ICmpInst::ICMP_NE:         // (X u> 13 | X != 15) -> true | 
|  | 3897 | case ICmpInst::ICMP_ULT:        // (X u> 13 | X u< 15) -> true | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3898 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3899 | case ICmpInst::ICMP_SLT:        // (X u> 13 | X s< 15) -> no change | 
|  | 3900 | break; | 
|  | 3901 | } | 
|  | 3902 | break; | 
|  | 3903 | case ICmpInst::ICMP_SGT: | 
|  | 3904 | switch (RHSCC) { | 
|  | 3905 | default: assert(0 && "Unknown integer condition code!"); | 
|  | 3906 | case ICmpInst::ICMP_EQ:         // (X s> 13 | X == 15) -> X > 13 | 
|  | 3907 | case ICmpInst::ICMP_SGT:        // (X s> 13 | X s> 15) -> X > 13 | 
|  | 3908 | return ReplaceInstUsesWith(I, LHS); | 
|  | 3909 | case ICmpInst::ICMP_UGT:        // (X s> 13 | X u> 15) -> no change | 
|  | 3910 | break; | 
|  | 3911 | case ICmpInst::ICMP_NE:         // (X s> 13 | X != 15) -> true | 
|  | 3912 | case ICmpInst::ICMP_SLT:        // (X s> 13 | X s< 15) -> true | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3913 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3914 | case ICmpInst::ICMP_ULT:        // (X s> 13 | X u< 15) -> no change | 
|  | 3915 | break; | 
|  | 3916 | } | 
|  | 3917 | break; | 
| Chris Lattner | dcf756e | 2004-09-28 22:33:08 +0000 | [diff] [blame] | 3918 | } | 
|  | 3919 | } | 
|  | 3920 | } | 
| Chris Lattner | 3af1053 | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3921 |  | 
|  | 3922 | // fold (or (cast A), (cast B)) -> (cast (or A, B)) | 
| Reid Spencer | 799b5bf | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 3923 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) | 
| Chris Lattner | 3af1053 | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3924 | if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) | 
| Reid Spencer | 799b5bf | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 3925 | if (Op0C->getOpcode() == Op1C->getOpcode()) {// same cast kind ? | 
|  | 3926 | const Type *SrcTy = Op0C->getOperand(0)->getType(); | 
| Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 3927 | if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() && | 
| Reid Spencer | 799b5bf | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 3928 | // Only do this if the casts both really cause code to be generated. | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3929 | ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0), | 
|  | 3930 | I.getType(), TD) && | 
|  | 3931 | ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0), | 
|  | 3932 | I.getType(), TD)) { | 
| Reid Spencer | 799b5bf | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 3933 | Instruction *NewOp = BinaryOperator::createOr(Op0C->getOperand(0), | 
|  | 3934 | Op1C->getOperand(0), | 
|  | 3935 | I.getName()); | 
|  | 3936 | InsertNewInstBefore(NewOp, I); | 
|  | 3937 | return CastInst::create(Op0C->getOpcode(), NewOp, I.getType()); | 
|  | 3938 | } | 
| Chris Lattner | 3af1053 | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3939 | } | 
| Chris Lattner | 3af1053 | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 3940 |  | 
| Chris Lattner | 1521298 | 2005-09-18 03:42:07 +0000 | [diff] [blame] | 3941 |  | 
| Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3942 | return Changed ? &I : 0; | 
| Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3943 | } | 
|  | 3944 |  | 
| Chris Lattner | c207635 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 3945 | // XorSelf - Implements: X ^ X --> 0 | 
|  | 3946 | struct XorSelf { | 
|  | 3947 | Value *RHS; | 
|  | 3948 | XorSelf(Value *rhs) : RHS(rhs) {} | 
|  | 3949 | bool shouldApply(Value *LHS) const { return LHS == RHS; } | 
|  | 3950 | Instruction *apply(BinaryOperator &Xor) const { | 
|  | 3951 | return &Xor; | 
|  | 3952 | } | 
|  | 3953 | }; | 
| Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3954 |  | 
|  | 3955 |  | 
| Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3956 | Instruction *InstCombiner::visitXor(BinaryOperator &I) { | 
| Chris Lattner | dcf240a | 2003-03-10 21:43:22 +0000 | [diff] [blame] | 3957 | bool Changed = SimplifyCommutative(I); | 
| Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 3958 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); | 
| Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3959 |  | 
| Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 3960 | if (isa<UndefValue>(Op1)) | 
|  | 3961 | return ReplaceInstUsesWith(I, Op1);  // X ^ undef -> undef | 
|  | 3962 |  | 
| Chris Lattner | c207635 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 3963 | // xor X, X = 0, even if X is nested in a sequence of Xor's. | 
|  | 3964 | if (Instruction *Result = AssociativeOpt(I, XorSelf(Op1))) { | 
|  | 3965 | assert(Result == &I && "AssociativeOpt didn't work?"); | 
| Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 3966 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); | 
| Chris Lattner | c207635 | 2004-02-16 01:20:27 +0000 | [diff] [blame] | 3967 | } | 
| Chris Lattner | 5b2edb1 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 3968 |  | 
|  | 3969 | // See if we can simplify any instructions used by the instruction whose sole | 
|  | 3970 | // purpose is to compute bits we don't care about. | 
| Reid Spencer | b722f2b | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 3971 | if (!isa<VectorType>(I.getType())) { | 
|  | 3972 | uint32_t BitWidth = cast<IntegerType>(I.getType())->getBitWidth(); | 
|  | 3973 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); | 
|  | 3974 | if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(BitWidth), | 
|  | 3975 | KnownZero, KnownOne)) | 
|  | 3976 | return &I; | 
|  | 3977 | } | 
| Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 3978 |  | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3979 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3980 | // xor (icmp A, B), true = not (icmp A, B) = !icmp A, B | 
|  | 3981 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(Op0)) | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3982 | if (RHS == ConstantInt::getTrue() && ICI->hasOneUse()) | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3983 | return new ICmpInst(ICI->getInversePredicate(), | 
|  | 3984 | ICI->getOperand(0), ICI->getOperand(1)); | 
| Chris Lattner | e580666 | 2003-11-04 23:50:51 +0000 | [diff] [blame] | 3985 |  | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3986 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) { | 
| Chris Lattner | 8f2f598 | 2003-11-05 01:06:05 +0000 | [diff] [blame] | 3987 | // ~(c-X) == X-c-1 == X+(-c-1) | 
| Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 3988 | if (Op0I->getOpcode() == Instruction::Sub && RHS->isAllOnesValue()) | 
|  | 3989 | if (Constant *Op0I0C = dyn_cast<Constant>(Op0I->getOperand(0))) { | 
| Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3990 | Constant *NegOp0I0C = ConstantExpr::getNeg(Op0I0C); | 
|  | 3991 | Constant *ConstantRHS = ConstantExpr::getSub(NegOp0I0C, | 
| Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 3992 | ConstantInt::get(I.getType(), 1)); | 
| Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 3993 | return BinaryOperator::createAdd(Op0I->getOperand(1), ConstantRHS); | 
| Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 3994 | } | 
| Chris Lattner | 023a483 | 2004-06-18 06:07:51 +0000 | [diff] [blame] | 3995 |  | 
|  | 3996 | // ~(~X & Y) --> (X | ~Y) | 
|  | 3997 | if (Op0I->getOpcode() == Instruction::And && RHS->isAllOnesValue()) { | 
|  | 3998 | if (dyn_castNotVal(Op0I->getOperand(1))) Op0I->swapOperands(); | 
|  | 3999 | if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0))) { | 
|  | 4000 | Instruction *NotY = | 
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4001 | BinaryOperator::createNot(Op0I->getOperand(1), | 
| Chris Lattner | 023a483 | 2004-06-18 06:07:51 +0000 | [diff] [blame] | 4002 | Op0I->getOperand(1)->getName()+".not"); | 
|  | 4003 | InsertNewInstBefore(NotY, I); | 
|  | 4004 | return BinaryOperator::createOr(Op0NotVal, NotY); | 
|  | 4005 | } | 
|  | 4006 | } | 
| Chris Lattner | b24acc7 | 2007-04-02 05:36:22 +0000 | [diff] [blame] | 4007 |  | 
| Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 4008 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) | 
| Chris Lattner | 5b2edb1 | 2006-02-12 08:02:11 +0000 | [diff] [blame] | 4009 | if (Op0I->getOpcode() == Instruction::Add) { | 
| Chris Lattner | 0f68fa6 | 2003-11-04 23:37:10 +0000 | [diff] [blame] | 4010 | // ~(X-c) --> (-c-1)-X | 
| Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4011 | if (RHS->isAllOnesValue()) { | 
| Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4012 | Constant *NegOp0CI = ConstantExpr::getNeg(Op0CI); | 
|  | 4013 | return BinaryOperator::createSub( | 
|  | 4014 | ConstantExpr::getSub(NegOp0CI, | 
| Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4015 | ConstantInt::get(I.getType(), 1)), | 
| Chris Lattner | 0f68fa6 | 2003-11-04 23:37:10 +0000 | [diff] [blame] | 4016 | Op0I->getOperand(0)); | 
| Chris Lattner | 50490d5 | 2007-04-02 05:42:22 +0000 | [diff] [blame] | 4017 | } else if (RHS->getValue().isSignBit()) { | 
| Chris Lattner | b24acc7 | 2007-04-02 05:36:22 +0000 | [diff] [blame] | 4018 | // (X + C) ^ signbit -> (X + C + signbit) | 
|  | 4019 | Constant *C = ConstantInt::get(RHS->getValue() + Op0CI->getValue()); | 
|  | 4020 | return BinaryOperator::createAdd(Op0I->getOperand(0), C); | 
| Chris Lattner | 9d5aace | 2007-04-02 05:48:58 +0000 | [diff] [blame] | 4021 |  | 
| Chris Lattner | c1e7cc0 | 2004-01-12 19:35:11 +0000 | [diff] [blame] | 4022 | } | 
| Chris Lattner | f78df7c | 2006-02-26 19:57:54 +0000 | [diff] [blame] | 4023 | } else if (Op0I->getOpcode() == Instruction::Or) { | 
|  | 4024 | // (X|C1)^C2 -> X^(C1|C2) iff X&~C1 == 0 | 
| Reid Spencer | b722f2b | 2007-03-22 22:19:58 +0000 | [diff] [blame] | 4025 | if (MaskedValueIsZero(Op0I->getOperand(0), Op0CI->getValue())) { | 
| Chris Lattner | f78df7c | 2006-02-26 19:57:54 +0000 | [diff] [blame] | 4026 | Constant *NewRHS = ConstantExpr::getOr(Op0CI, RHS); | 
|  | 4027 | // Anything in both C1 and C2 is known to be zero, remove it from | 
|  | 4028 | // NewRHS. | 
| Zhou Sheng | 9bc8ab1 | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 4029 | Constant *CommonBits = And(Op0CI, RHS); | 
| Chris Lattner | f78df7c | 2006-02-26 19:57:54 +0000 | [diff] [blame] | 4030 | NewRHS = ConstantExpr::getAnd(NewRHS, | 
|  | 4031 | ConstantExpr::getNot(CommonBits)); | 
| Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 4032 | AddToWorkList(Op0I); | 
| Chris Lattner | f78df7c | 2006-02-26 19:57:54 +0000 | [diff] [blame] | 4033 | I.setOperand(0, Op0I->getOperand(0)); | 
|  | 4034 | I.setOperand(1, NewRHS); | 
|  | 4035 | return &I; | 
|  | 4036 | } | 
| Chris Lattner | 9763859 | 2003-07-23 21:37:07 +0000 | [diff] [blame] | 4037 | } | 
| Chris Lattner | b8d6e40 | 2002-08-20 18:24:26 +0000 | [diff] [blame] | 4038 | } | 
| Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 4039 |  | 
|  | 4040 | // Try to fold constant and into select arguments. | 
|  | 4041 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) | 
| Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 4042 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) | 
| Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 4043 | return R; | 
| Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 4044 | if (isa<PHINode>(Op0)) | 
|  | 4045 | if (Instruction *NV = FoldOpIntoPhi(I)) | 
|  | 4046 | return NV; | 
| Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4047 | } | 
|  | 4048 |  | 
| Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 4049 | if (Value *X = dyn_castNotVal(Op0))   // ~A ^ A == -1 | 
| Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 4050 | if (X == Op1) | 
|  | 4051 | return ReplaceInstUsesWith(I, | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4052 | ConstantInt::getAllOnesValue(I.getType())); | 
| Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 4053 |  | 
| Chris Lattner | bb74e22 | 2003-03-10 23:06:50 +0000 | [diff] [blame] | 4054 | if (Value *X = dyn_castNotVal(Op1))   // A ^ ~A == -1 | 
| Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 4055 | if (X == Op0) | 
| Chris Lattner | 0741842 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4056 | return ReplaceInstUsesWith(I, ConstantInt::getAllOnesValue(I.getType())); | 
| Chris Lattner | 3082c5a | 2003-02-18 19:28:33 +0000 | [diff] [blame] | 4057 |  | 
| Chris Lattner | 0741842 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4058 |  | 
|  | 4059 | BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1); | 
|  | 4060 | if (Op1I) { | 
|  | 4061 | Value *A, *B; | 
|  | 4062 | if (match(Op1I, m_Or(m_Value(A), m_Value(B)))) { | 
|  | 4063 | if (A == Op0) {              // B^(B|A) == (A|B)^B | 
| Chris Lattner | dcd0792 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4064 | Op1I->swapOperands(); | 
| Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 4065 | I.swapOperands(); | 
|  | 4066 | std::swap(Op0, Op1); | 
| Chris Lattner | 0741842 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4067 | } else if (B == Op0) {       // B^(A|B) == (A|B)^B | 
| Chris Lattner | dcd0792 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4068 | I.swapOperands();     // Simplified below. | 
| Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 4069 | std::swap(Op0, Op1); | 
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4070 | } | 
| Chris Lattner | 0741842 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4071 | } else if (match(Op1I, m_Xor(m_Value(A), m_Value(B)))) { | 
|  | 4072 | if (Op0 == A)                                          // A^(A^B) == B | 
|  | 4073 | return ReplaceInstUsesWith(I, B); | 
|  | 4074 | else if (Op0 == B)                                     // A^(B^A) == B | 
|  | 4075 | return ReplaceInstUsesWith(I, A); | 
|  | 4076 | } else if (match(Op1I, m_And(m_Value(A), m_Value(B))) && Op1I->hasOneUse()){ | 
| Chris Lattner | 0427799 | 2007-04-01 05:36:37 +0000 | [diff] [blame] | 4077 | if (A == Op0) {                                      // A^(A&B) -> A^(B&A) | 
| Chris Lattner | dcd0792 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4078 | Op1I->swapOperands(); | 
| Chris Lattner | 0427799 | 2007-04-01 05:36:37 +0000 | [diff] [blame] | 4079 | std::swap(A, B); | 
|  | 4080 | } | 
| Chris Lattner | 0741842 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4081 | if (B == Op0) {                                      // A^(B&A) -> (B&A)^A | 
| Chris Lattner | dcd0792 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4082 | I.swapOperands();     // Simplified below. | 
|  | 4083 | std::swap(Op0, Op1); | 
|  | 4084 | } | 
| Chris Lattner | b36d908 | 2004-02-16 03:54:20 +0000 | [diff] [blame] | 4085 | } | 
| Chris Lattner | 0741842 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4086 | } | 
|  | 4087 |  | 
|  | 4088 | BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0); | 
|  | 4089 | if (Op0I) { | 
|  | 4090 | Value *A, *B; | 
|  | 4091 | if (match(Op0I, m_Or(m_Value(A), m_Value(B))) && Op0I->hasOneUse()) { | 
|  | 4092 | if (A == Op1)                                  // (B|A)^B == (A|B)^B | 
|  | 4093 | std::swap(A, B); | 
|  | 4094 | if (B == Op1) {                                // (A|B)^B == A & ~B | 
|  | 4095 | Instruction *NotB = | 
|  | 4096 | InsertNewInstBefore(BinaryOperator::createNot(Op1, "tmp"), I); | 
|  | 4097 | return BinaryOperator::createAnd(A, NotB); | 
| Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 4098 | } | 
| Chris Lattner | 0741842 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4099 | } else if (match(Op0I, m_Xor(m_Value(A), m_Value(B)))) { | 
|  | 4100 | if (Op1 == A)                                          // (A^B)^A == B | 
|  | 4101 | return ReplaceInstUsesWith(I, B); | 
|  | 4102 | else if (Op1 == B)                                     // (B^A)^A == B | 
|  | 4103 | return ReplaceInstUsesWith(I, A); | 
|  | 4104 | } else if (match(Op0I, m_And(m_Value(A), m_Value(B))) && Op0I->hasOneUse()){ | 
|  | 4105 | if (A == Op1)                                        // (A&B)^A -> (B&A)^A | 
|  | 4106 | std::swap(A, B); | 
|  | 4107 | if (B == Op1 &&                                      // (B&A)^A == ~B & A | 
| Chris Lattner | 6cf4914 | 2006-04-01 22:05:01 +0000 | [diff] [blame] | 4108 | !isa<ConstantInt>(Op1)) {  // Canonical form is (B&C)^C | 
| Chris Lattner | 0741842 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4109 | Instruction *N = | 
|  | 4110 | InsertNewInstBefore(BinaryOperator::createNot(A, "tmp"), I); | 
| Chris Lattner | dcd0792 | 2006-04-01 08:03:55 +0000 | [diff] [blame] | 4111 | return BinaryOperator::createAnd(N, Op1); | 
|  | 4112 | } | 
| Chris Lattner | 1bbb7b6 | 2003-03-10 18:24:17 +0000 | [diff] [blame] | 4113 | } | 
| Chris Lattner | 0741842 | 2007-03-18 22:51:34 +0000 | [diff] [blame] | 4114 | } | 
|  | 4115 |  | 
|  | 4116 | // (X >> Z) ^ (Y >> Z)  -> (X^Y) >> Z  for all shifts. | 
|  | 4117 | if (Op0I && Op1I && Op0I->isShift() && | 
|  | 4118 | Op0I->getOpcode() == Op1I->getOpcode() && | 
|  | 4119 | Op0I->getOperand(1) == Op1I->getOperand(1) && | 
|  | 4120 | (Op1I->hasOneUse() || Op1I->hasOneUse())) { | 
|  | 4121 | Instruction *NewOp = | 
|  | 4122 | InsertNewInstBefore(BinaryOperator::createXor(Op0I->getOperand(0), | 
|  | 4123 | Op1I->getOperand(0), | 
|  | 4124 | Op0I->getName()), I); | 
|  | 4125 | return BinaryOperator::create(Op1I->getOpcode(), NewOp, | 
|  | 4126 | Op1I->getOperand(1)); | 
|  | 4127 | } | 
|  | 4128 |  | 
|  | 4129 | if (Op0I && Op1I) { | 
|  | 4130 | Value *A, *B, *C, *D; | 
|  | 4131 | // (A & B)^(A | B) -> A ^ B | 
|  | 4132 | if (match(Op0I, m_And(m_Value(A), m_Value(B))) && | 
|  | 4133 | match(Op1I, m_Or(m_Value(C), m_Value(D)))) { | 
|  | 4134 | if ((A == C && B == D) || (A == D && B == C)) | 
|  | 4135 | return BinaryOperator::createXor(A, B); | 
|  | 4136 | } | 
|  | 4137 | // (A | B)^(A & B) -> A ^ B | 
|  | 4138 | if (match(Op0I, m_Or(m_Value(A), m_Value(B))) && | 
|  | 4139 | match(Op1I, m_And(m_Value(C), m_Value(D)))) { | 
|  | 4140 | if ((A == C && B == D) || (A == D && B == C)) | 
|  | 4141 | return BinaryOperator::createXor(A, B); | 
|  | 4142 | } | 
|  | 4143 |  | 
|  | 4144 | // (A & B)^(C & D) | 
|  | 4145 | if ((Op0I->hasOneUse() || Op1I->hasOneUse()) && | 
|  | 4146 | match(Op0I, m_And(m_Value(A), m_Value(B))) && | 
|  | 4147 | match(Op1I, m_And(m_Value(C), m_Value(D)))) { | 
|  | 4148 | // (X & Y)^(X & Y) -> (Y^Z) & X | 
|  | 4149 | Value *X = 0, *Y = 0, *Z = 0; | 
|  | 4150 | if (A == C) | 
|  | 4151 | X = A, Y = B, Z = D; | 
|  | 4152 | else if (A == D) | 
|  | 4153 | X = A, Y = B, Z = C; | 
|  | 4154 | else if (B == C) | 
|  | 4155 | X = B, Y = A, Z = D; | 
|  | 4156 | else if (B == D) | 
|  | 4157 | X = B, Y = A, Z = C; | 
|  | 4158 |  | 
|  | 4159 | if (X) { | 
|  | 4160 | Instruction *NewOp = | 
|  | 4161 | InsertNewInstBefore(BinaryOperator::createXor(Y, Z, Op0->getName()), I); | 
|  | 4162 | return BinaryOperator::createAnd(NewOp, X); | 
|  | 4163 | } | 
|  | 4164 | } | 
|  | 4165 | } | 
|  | 4166 |  | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4167 | // (icmp1 A, B) ^ (icmp2 A, B) --> (icmp3 A, B) | 
|  | 4168 | if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1))) | 
|  | 4169 | if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS))) | 
| Chris Lattner | 3ac7c26 | 2003-08-13 20:16:26 +0000 | [diff] [blame] | 4170 | return R; | 
|  | 4171 |  | 
| Chris Lattner | 3af1053 | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4172 | // fold (xor (cast A), (cast B)) -> (cast (xor A, B)) | 
| Reid Spencer | 799b5bf | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4173 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) | 
| Chris Lattner | 3af1053 | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4174 | if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) | 
| Reid Spencer | 799b5bf | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4175 | if (Op0C->getOpcode() == Op1C->getOpcode()) { // same cast kind? | 
|  | 4176 | const Type *SrcTy = Op0C->getOperand(0)->getType(); | 
| Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 4177 | if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() && | 
| Reid Spencer | 799b5bf | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4178 | // Only do this if the casts both really cause code to be generated. | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4179 | ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0), | 
|  | 4180 | I.getType(), TD) && | 
|  | 4181 | ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0), | 
|  | 4182 | I.getType(), TD)) { | 
| Reid Spencer | 799b5bf | 2006-12-13 08:27:15 +0000 | [diff] [blame] | 4183 | Instruction *NewOp = BinaryOperator::createXor(Op0C->getOperand(0), | 
|  | 4184 | Op1C->getOperand(0), | 
|  | 4185 | I.getName()); | 
|  | 4186 | InsertNewInstBefore(NewOp, I); | 
|  | 4187 | return CastInst::create(Op0C->getOpcode(), NewOp, I.getType()); | 
|  | 4188 | } | 
| Chris Lattner | 3af1053 | 2006-05-05 06:39:07 +0000 | [diff] [blame] | 4189 | } | 
| Chris Lattner | f05d69a | 2006-11-14 07:46:50 +0000 | [diff] [blame] | 4190 |  | 
| Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4191 | return Changed ? &I : 0; | 
| Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4192 | } | 
|  | 4193 |  | 
| Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4194 | /// AddWithOverflow - Compute Result = In1+In2, returning true if the result | 
|  | 4195 | /// overflowed for this type. | 
|  | 4196 | static bool AddWithOverflow(ConstantInt *&Result, ConstantInt *In1, | 
| Reid Spencer | f407116 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 4197 | ConstantInt *In2, bool IsSigned = false) { | 
| Zhou Sheng | 9bc8ab1 | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 4198 | Result = cast<ConstantInt>(Add(In1, In2)); | 
| Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4199 |  | 
| Reid Spencer | f407116 | 2007-03-21 23:19:50 +0000 | [diff] [blame] | 4200 | if (IsSigned) | 
|  | 4201 | if (In2->getValue().isNegative()) | 
|  | 4202 | return Result->getValue().sgt(In1->getValue()); | 
|  | 4203 | else | 
|  | 4204 | return Result->getValue().slt(In1->getValue()); | 
|  | 4205 | else | 
|  | 4206 | return Result->getValue().ult(In1->getValue()); | 
| Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4207 | } | 
|  | 4208 |  | 
| Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4209 | /// EmitGEPOffset - Given a getelementptr instruction/constantexpr, emit the | 
|  | 4210 | /// code necessary to compute the offset from the base pointer (without adding | 
|  | 4211 | /// in the base pointer).  Return the result as a signed integer of intptr size. | 
|  | 4212 | static Value *EmitGEPOffset(User *GEP, Instruction &I, InstCombiner &IC) { | 
|  | 4213 | TargetData &TD = IC.getTargetData(); | 
|  | 4214 | gep_type_iterator GTI = gep_type_begin(GEP); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4215 | const Type *IntPtrTy = TD.getIntPtrType(); | 
|  | 4216 | Value *Result = Constant::getNullValue(IntPtrTy); | 
| Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4217 |  | 
|  | 4218 | // Build a mask for high order bits. | 
| Chris Lattner | 77defba | 2006-02-07 07:00:41 +0000 | [diff] [blame] | 4219 | uint64_t PtrSizeMask = ~0ULL >> (64-TD.getPointerSize()*8); | 
| Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4220 |  | 
| Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4221 | for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i, ++GTI) { | 
|  | 4222 | Value *Op = GEP->getOperand(i); | 
| Chris Lattner | d35d210 | 2005-01-13 23:26:48 +0000 | [diff] [blame] | 4223 | uint64_t Size = TD.getTypeSize(GTI.getIndexedType()) & PtrSizeMask; | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4224 | Constant *Scale = ConstantInt::get(IntPtrTy, Size); | 
| Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4225 | if (Constant *OpC = dyn_cast<Constant>(Op)) { | 
|  | 4226 | if (!OpC->isNullValue()) { | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4227 | OpC = ConstantExpr::getIntegerCast(OpC, IntPtrTy, true /*SExt*/); | 
| Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4228 | Scale = ConstantExpr::getMul(OpC, Scale); | 
|  | 4229 | if (Constant *RC = dyn_cast<Constant>(Result)) | 
|  | 4230 | Result = ConstantExpr::getAdd(RC, Scale); | 
|  | 4231 | else { | 
|  | 4232 | // Emit an add instruction. | 
|  | 4233 | Result = IC.InsertNewInstBefore( | 
|  | 4234 | BinaryOperator::createAdd(Result, Scale, | 
|  | 4235 | GEP->getName()+".offs"), I); | 
|  | 4236 | } | 
|  | 4237 | } | 
|  | 4238 | } else { | 
| Chris Lattner | 7aa41cf | 2005-01-14 17:17:59 +0000 | [diff] [blame] | 4239 | // Convert to correct type. | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4240 | Op = IC.InsertNewInstBefore(CastInst::createSExtOrBitCast(Op, IntPtrTy, | 
| Chris Lattner | 7aa41cf | 2005-01-14 17:17:59 +0000 | [diff] [blame] | 4241 | Op->getName()+".c"), I); | 
|  | 4242 | if (Size != 1) | 
| Chris Lattner | 4cb9fa3 | 2005-01-13 20:40:58 +0000 | [diff] [blame] | 4243 | // We'll let instcombine(mul) convert this to a shl if possible. | 
|  | 4244 | Op = IC.InsertNewInstBefore(BinaryOperator::createMul(Op, Scale, | 
|  | 4245 | GEP->getName()+".idx"), I); | 
| Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4246 |  | 
|  | 4247 | // Emit an add instruction. | 
| Chris Lattner | 4cb9fa3 | 2005-01-13 20:40:58 +0000 | [diff] [blame] | 4248 | Result = IC.InsertNewInstBefore(BinaryOperator::createAdd(Op, Result, | 
| Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4249 | GEP->getName()+".offs"), I); | 
|  | 4250 | } | 
|  | 4251 | } | 
|  | 4252 | return Result; | 
|  | 4253 | } | 
|  | 4254 |  | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4255 | /// FoldGEPICmp - Fold comparisons between a GEP instruction and something | 
| Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4256 | /// else.  At this point we know that the GEP is on the LHS of the comparison. | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4257 | Instruction *InstCombiner::FoldGEPICmp(User *GEPLHS, Value *RHS, | 
|  | 4258 | ICmpInst::Predicate Cond, | 
|  | 4259 | Instruction &I) { | 
| Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4260 | assert(dyn_castGetElementPtr(GEPLHS) && "LHS is not a getelementptr!"); | 
| Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4261 |  | 
|  | 4262 | if (CastInst *CI = dyn_cast<CastInst>(RHS)) | 
|  | 4263 | if (isa<PointerType>(CI->getOperand(0)->getType())) | 
|  | 4264 | RHS = CI->getOperand(0); | 
|  | 4265 |  | 
| Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4266 | Value *PtrBase = GEPLHS->getOperand(0); | 
|  | 4267 | if (PtrBase == RHS) { | 
|  | 4268 | // As an optimization, we don't actually have to compute the actual value of | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4269 | // OFFSET if this is a icmp_eq or icmp_ne comparison, just return whether | 
|  | 4270 | // each index is zero or not. | 
|  | 4271 | if (Cond == ICmpInst::ICMP_EQ || Cond == ICmpInst::ICMP_NE) { | 
| Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4272 | Instruction *InVal = 0; | 
| Chris Lattner | cd517ff | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 4273 | gep_type_iterator GTI = gep_type_begin(GEPLHS); | 
|  | 4274 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i, ++GTI) { | 
| Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4275 | bool EmitIt = true; | 
|  | 4276 | if (Constant *C = dyn_cast<Constant>(GEPLHS->getOperand(i))) { | 
|  | 4277 | if (isa<UndefValue>(C))  // undef index -> undef. | 
|  | 4278 | return ReplaceInstUsesWith(I, UndefValue::get(I.getType())); | 
|  | 4279 | if (C->isNullValue()) | 
|  | 4280 | EmitIt = false; | 
| Chris Lattner | cd517ff | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 4281 | else if (TD->getTypeSize(GTI.getIndexedType()) == 0) { | 
|  | 4282 | EmitIt = false;  // This is indexing into a zero sized array? | 
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4283 | } else if (isa<ConstantInt>(C)) | 
| Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4284 | return ReplaceInstUsesWith(I, // No comparison is needed here. | 
| Reid Spencer | cddc9df | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 4285 | ConstantInt::get(Type::Int1Ty, | 
|  | 4286 | Cond == ICmpInst::ICMP_NE)); | 
| Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4287 | } | 
|  | 4288 |  | 
|  | 4289 | if (EmitIt) { | 
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4290 | Instruction *Comp = | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4291 | new ICmpInst(Cond, GEPLHS->getOperand(i), | 
| Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4292 | Constant::getNullValue(GEPLHS->getOperand(i)->getType())); | 
|  | 4293 | if (InVal == 0) | 
|  | 4294 | InVal = Comp; | 
|  | 4295 | else { | 
|  | 4296 | InVal = InsertNewInstBefore(InVal, I); | 
|  | 4297 | InsertNewInstBefore(Comp, I); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4298 | if (Cond == ICmpInst::ICMP_NE)   // True if any are unequal | 
| Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4299 | InVal = BinaryOperator::createOr(InVal, Comp); | 
|  | 4300 | else                              // True if all are equal | 
|  | 4301 | InVal = BinaryOperator::createAnd(InVal, Comp); | 
|  | 4302 | } | 
|  | 4303 | } | 
|  | 4304 | } | 
|  | 4305 |  | 
|  | 4306 | if (InVal) | 
|  | 4307 | return InVal; | 
|  | 4308 | else | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4309 | // No comparison is needed here, all indexes = 0 | 
| Reid Spencer | cddc9df | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 4310 | ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, | 
|  | 4311 | Cond == ICmpInst::ICMP_EQ)); | 
| Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4312 | } | 
| Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4313 |  | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4314 | // Only lower this if the icmp is the only user of the GEP or if we expect | 
| Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4315 | // the result to fold to a constant! | 
|  | 4316 | if (isa<ConstantExpr>(GEPLHS) || GEPLHS->hasOneUse()) { | 
|  | 4317 | // ((gep Ptr, OFFSET) cmp Ptr)   ---> (OFFSET cmp 0). | 
|  | 4318 | Value *Offset = EmitGEPOffset(GEPLHS, I, *this); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4319 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), Offset, | 
|  | 4320 | Constant::getNullValue(Offset->getType())); | 
| Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4321 | } | 
|  | 4322 | } else if (User *GEPRHS = dyn_castGetElementPtr(RHS)) { | 
| Chris Lattner | a21bf8d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 4323 | // If the base pointers are different, but the indices are the same, just | 
|  | 4324 | // compare the base pointer. | 
|  | 4325 | if (PtrBase != GEPRHS->getOperand(0)) { | 
|  | 4326 | bool IndicesTheSame = GEPLHS->getNumOperands()==GEPRHS->getNumOperands(); | 
| Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 4327 | IndicesTheSame &= GEPLHS->getOperand(0)->getType() == | 
| Chris Lattner | bd43b9d | 2005-04-26 14:40:41 +0000 | [diff] [blame] | 4328 | GEPRHS->getOperand(0)->getType(); | 
| Chris Lattner | a21bf8d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 4329 | if (IndicesTheSame) | 
|  | 4330 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i) | 
|  | 4331 | if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) { | 
|  | 4332 | IndicesTheSame = false; | 
|  | 4333 | break; | 
|  | 4334 | } | 
|  | 4335 |  | 
|  | 4336 | // If all indices are the same, just compare the base pointers. | 
|  | 4337 | if (IndicesTheSame) | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4338 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), | 
|  | 4339 | GEPLHS->getOperand(0), GEPRHS->getOperand(0)); | 
| Chris Lattner | a21bf8d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 4340 |  | 
|  | 4341 | // Otherwise, the base pointers are different and the indices are | 
|  | 4342 | // different, bail out. | 
| Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4343 | return 0; | 
| Chris Lattner | a21bf8d | 2005-04-25 20:17:30 +0000 | [diff] [blame] | 4344 | } | 
| Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4345 |  | 
| Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4346 | // If one of the GEPs has all zero indices, recurse. | 
|  | 4347 | bool AllZeros = true; | 
|  | 4348 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i) | 
|  | 4349 | if (!isa<Constant>(GEPLHS->getOperand(i)) || | 
|  | 4350 | !cast<Constant>(GEPLHS->getOperand(i))->isNullValue()) { | 
|  | 4351 | AllZeros = false; | 
|  | 4352 | break; | 
|  | 4353 | } | 
|  | 4354 | if (AllZeros) | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4355 | return FoldGEPICmp(GEPRHS, GEPLHS->getOperand(0), | 
|  | 4356 | ICmpInst::getSwappedPredicate(Cond), I); | 
| Chris Lattner | 4fa8982 | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 4357 |  | 
|  | 4358 | // If the other GEP has all zero indices, recurse. | 
| Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4359 | AllZeros = true; | 
|  | 4360 | for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i) | 
|  | 4361 | if (!isa<Constant>(GEPRHS->getOperand(i)) || | 
|  | 4362 | !cast<Constant>(GEPRHS->getOperand(i))->isNullValue()) { | 
|  | 4363 | AllZeros = false; | 
|  | 4364 | break; | 
|  | 4365 | } | 
|  | 4366 | if (AllZeros) | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4367 | return FoldGEPICmp(GEPLHS, GEPRHS->getOperand(0), Cond, I); | 
| Chris Lattner | 81e8417 | 2005-01-13 22:25:21 +0000 | [diff] [blame] | 4368 |  | 
| Chris Lattner | 4fa8982 | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 4369 | if (GEPLHS->getNumOperands() == GEPRHS->getNumOperands()) { | 
|  | 4370 | // If the GEPs only differ by one index, compare it. | 
|  | 4371 | unsigned NumDifferences = 0;  // Keep track of # differences. | 
|  | 4372 | unsigned DiffOperand = 0;     // The operand that differs. | 
|  | 4373 | for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i) | 
|  | 4374 | if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) { | 
| Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 4375 | if (GEPLHS->getOperand(i)->getType()->getPrimitiveSizeInBits() != | 
|  | 4376 | GEPRHS->getOperand(i)->getType()->getPrimitiveSizeInBits()) { | 
| Chris Lattner | fc4429e | 2005-01-21 23:06:49 +0000 | [diff] [blame] | 4377 | // Irreconcilable differences. | 
| Chris Lattner | 4fa8982 | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 4378 | NumDifferences = 2; | 
|  | 4379 | break; | 
|  | 4380 | } else { | 
|  | 4381 | if (NumDifferences++) break; | 
|  | 4382 | DiffOperand = i; | 
|  | 4383 | } | 
|  | 4384 | } | 
|  | 4385 |  | 
|  | 4386 | if (NumDifferences == 0)   // SAME GEP? | 
|  | 4387 | return ReplaceInstUsesWith(I, // No comparison is needed here. | 
| Reid Spencer | cddc9df | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 4388 | ConstantInt::get(Type::Int1Ty, | 
|  | 4389 | Cond == ICmpInst::ICMP_EQ)); | 
| Chris Lattner | 4fa8982 | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 4390 | else if (NumDifferences == 1) { | 
| Chris Lattner | fc4429e | 2005-01-21 23:06:49 +0000 | [diff] [blame] | 4391 | Value *LHSV = GEPLHS->getOperand(DiffOperand); | 
|  | 4392 | Value *RHSV = GEPRHS->getOperand(DiffOperand); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4393 | // Make sure we do a signed comparison here. | 
|  | 4394 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), LHSV, RHSV); | 
| Chris Lattner | 4fa8982 | 2005-01-14 00:20:05 +0000 | [diff] [blame] | 4395 | } | 
|  | 4396 | } | 
|  | 4397 |  | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4398 | // Only lower this if the icmp is the only user of the GEP or if we expect | 
| Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4399 | // the result to fold to a constant! | 
|  | 4400 | if ((isa<ConstantExpr>(GEPLHS) || GEPLHS->hasOneUse()) && | 
|  | 4401 | (isa<ConstantExpr>(GEPRHS) || GEPRHS->hasOneUse())) { | 
|  | 4402 | // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2)  --->  (OFFSET1 cmp OFFSET2) | 
|  | 4403 | Value *L = EmitGEPOffset(GEPLHS, I, *this); | 
|  | 4404 | Value *R = EmitGEPOffset(GEPRHS, I, *this); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4405 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), L, R); | 
| Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4406 | } | 
|  | 4407 | } | 
|  | 4408 | return 0; | 
|  | 4409 | } | 
|  | 4410 |  | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4411 | Instruction *InstCombiner::visitFCmpInst(FCmpInst &I) { | 
|  | 4412 | bool Changed = SimplifyCompare(I); | 
| Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4413 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); | 
| Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4414 |  | 
| Chris Lattner | 6ee923f | 2007-01-14 19:42:17 +0000 | [diff] [blame] | 4415 | // Fold trivial predicates. | 
|  | 4416 | if (I.getPredicate() == FCmpInst::FCMP_FALSE) | 
|  | 4417 | return ReplaceInstUsesWith(I, Constant::getNullValue(Type::Int1Ty)); | 
|  | 4418 | if (I.getPredicate() == FCmpInst::FCMP_TRUE) | 
|  | 4419 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 1)); | 
|  | 4420 |  | 
|  | 4421 | // Simplify 'fcmp pred X, X' | 
|  | 4422 | if (Op0 == Op1) { | 
|  | 4423 | switch (I.getPredicate()) { | 
|  | 4424 | default: assert(0 && "Unknown predicate!"); | 
|  | 4425 | case FCmpInst::FCMP_UEQ:    // True if unordered or equal | 
|  | 4426 | case FCmpInst::FCMP_UGE:    // True if unordered, greater than, or equal | 
|  | 4427 | case FCmpInst::FCMP_ULE:    // True if unordered, less than, or equal | 
|  | 4428 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 1)); | 
|  | 4429 | case FCmpInst::FCMP_OGT:    // True if ordered and greater than | 
|  | 4430 | case FCmpInst::FCMP_OLT:    // True if ordered and less than | 
|  | 4431 | case FCmpInst::FCMP_ONE:    // True if ordered and operands are unequal | 
|  | 4432 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, 0)); | 
|  | 4433 |  | 
|  | 4434 | case FCmpInst::FCMP_UNO:    // True if unordered: isnan(X) | isnan(Y) | 
|  | 4435 | case FCmpInst::FCMP_ULT:    // True if unordered or less than | 
|  | 4436 | case FCmpInst::FCMP_UGT:    // True if unordered or greater than | 
|  | 4437 | case FCmpInst::FCMP_UNE:    // True if unordered or not equal | 
|  | 4438 | // Canonicalize these to be 'fcmp uno %X, 0.0'. | 
|  | 4439 | I.setPredicate(FCmpInst::FCMP_UNO); | 
|  | 4440 | I.setOperand(1, Constant::getNullValue(Op0->getType())); | 
|  | 4441 | return &I; | 
|  | 4442 |  | 
|  | 4443 | case FCmpInst::FCMP_ORD:    // True if ordered (no nans) | 
|  | 4444 | case FCmpInst::FCMP_OEQ:    // True if ordered and equal | 
|  | 4445 | case FCmpInst::FCMP_OGE:    // True if ordered and greater than or equal | 
|  | 4446 | case FCmpInst::FCMP_OLE:    // True if ordered and less than or equal | 
|  | 4447 | // Canonicalize these to be 'fcmp ord %X, 0.0'. | 
|  | 4448 | I.setPredicate(FCmpInst::FCMP_ORD); | 
|  | 4449 | I.setOperand(1, Constant::getNullValue(Op0->getType())); | 
|  | 4450 | return &I; | 
|  | 4451 | } | 
|  | 4452 | } | 
|  | 4453 |  | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4454 | if (isa<UndefValue>(Op1))                  // fcmp pred X, undef -> undef | 
| Reid Spencer | 542964f | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 4455 | return ReplaceInstUsesWith(I, UndefValue::get(Type::Int1Ty)); | 
| Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 4456 |  | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4457 | // Handle fcmp with constant RHS | 
|  | 4458 | if (Constant *RHSC = dyn_cast<Constant>(Op1)) { | 
|  | 4459 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) | 
|  | 4460 | switch (LHSI->getOpcode()) { | 
|  | 4461 | case Instruction::PHI: | 
|  | 4462 | if (Instruction *NV = FoldOpIntoPhi(I)) | 
|  | 4463 | return NV; | 
|  | 4464 | break; | 
|  | 4465 | case Instruction::Select: | 
|  | 4466 | // If either operand of the select is a constant, we can fold the | 
|  | 4467 | // comparison into the select arms, which will cause one to be | 
|  | 4468 | // constant folded and the select turned into a bitwise or. | 
|  | 4469 | Value *Op1 = 0, *Op2 = 0; | 
|  | 4470 | if (LHSI->hasOneUse()) { | 
|  | 4471 | if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) { | 
|  | 4472 | // Fold the known value into the constant operand. | 
|  | 4473 | Op1 = ConstantExpr::getCompare(I.getPredicate(), C, RHSC); | 
|  | 4474 | // Insert a new FCmp of the other select operand. | 
|  | 4475 | Op2 = InsertNewInstBefore(new FCmpInst(I.getPredicate(), | 
|  | 4476 | LHSI->getOperand(2), RHSC, | 
|  | 4477 | I.getName()), I); | 
|  | 4478 | } else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) { | 
|  | 4479 | // Fold the known value into the constant operand. | 
|  | 4480 | Op2 = ConstantExpr::getCompare(I.getPredicate(), C, RHSC); | 
|  | 4481 | // Insert a new FCmp of the other select operand. | 
|  | 4482 | Op1 = InsertNewInstBefore(new FCmpInst(I.getPredicate(), | 
|  | 4483 | LHSI->getOperand(1), RHSC, | 
|  | 4484 | I.getName()), I); | 
|  | 4485 | } | 
|  | 4486 | } | 
|  | 4487 |  | 
|  | 4488 | if (Op1) | 
|  | 4489 | return new SelectInst(LHSI->getOperand(0), Op1, Op2); | 
|  | 4490 | break; | 
|  | 4491 | } | 
|  | 4492 | } | 
|  | 4493 |  | 
|  | 4494 | return Changed ? &I : 0; | 
|  | 4495 | } | 
|  | 4496 |  | 
|  | 4497 | Instruction *InstCombiner::visitICmpInst(ICmpInst &I) { | 
|  | 4498 | bool Changed = SimplifyCompare(I); | 
|  | 4499 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); | 
|  | 4500 | const Type *Ty = Op0->getType(); | 
|  | 4501 |  | 
|  | 4502 | // icmp X, X | 
|  | 4503 | if (Op0 == Op1) | 
| Reid Spencer | cddc9df | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 4504 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, | 
|  | 4505 | isTrueWhenEqual(I))); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4506 |  | 
|  | 4507 | if (isa<UndefValue>(Op1))                  // X icmp undef -> undef | 
| Reid Spencer | 542964f | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 4508 | return ReplaceInstUsesWith(I, UndefValue::get(Type::Int1Ty)); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4509 |  | 
|  | 4510 | // icmp of GlobalValues can never equal each other as long as they aren't | 
|  | 4511 | // external weak linkage type. | 
|  | 4512 | if (GlobalValue *GV0 = dyn_cast<GlobalValue>(Op0)) | 
|  | 4513 | if (GlobalValue *GV1 = dyn_cast<GlobalValue>(Op1)) | 
|  | 4514 | if (!GV0->hasExternalWeakLinkage() || !GV1->hasExternalWeakLinkage()) | 
| Reid Spencer | cddc9df | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 4515 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, | 
|  | 4516 | !isTrueWhenEqual(I))); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4517 |  | 
|  | 4518 | // icmp <global/alloca*/null>, <global/alloca*/null> - Global/Stack value | 
| Chris Lattner | 15ff1e1 | 2004-11-14 07:33:16 +0000 | [diff] [blame] | 4519 | // addresses never equal each other!  We already know that Op0 != Op1. | 
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4520 | if ((isa<GlobalValue>(Op0) || isa<AllocaInst>(Op0) || | 
|  | 4521 | isa<ConstantPointerNull>(Op0)) && | 
|  | 4522 | (isa<GlobalValue>(Op1) || isa<AllocaInst>(Op1) || | 
| Chris Lattner | 15ff1e1 | 2004-11-14 07:33:16 +0000 | [diff] [blame] | 4523 | isa<ConstantPointerNull>(Op1))) | 
| Reid Spencer | cddc9df | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 4524 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, | 
|  | 4525 | !isTrueWhenEqual(I))); | 
| Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4526 |  | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4527 | // icmp's with boolean values can always be turned into bitwise operations | 
| Reid Spencer | 542964f | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 4528 | if (Ty == Type::Int1Ty) { | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4529 | switch (I.getPredicate()) { | 
|  | 4530 | default: assert(0 && "Invalid icmp instruction!"); | 
|  | 4531 | case ICmpInst::ICMP_EQ: {               // icmp eq bool %A, %B -> ~(A^B) | 
| Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 4532 | Instruction *Xor = BinaryOperator::createXor(Op0, Op1, I.getName()+"tmp"); | 
| Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4533 | InsertNewInstBefore(Xor, I); | 
| Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 4534 | return BinaryOperator::createNot(Xor); | 
| Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4535 | } | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4536 | case ICmpInst::ICMP_NE:                  // icmp eq bool %A, %B -> A^B | 
| Chris Lattner | 4456da6 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 4537 | return BinaryOperator::createXor(Op0, Op1); | 
| Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4538 |  | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4539 | case ICmpInst::ICMP_UGT: | 
|  | 4540 | case ICmpInst::ICMP_SGT: | 
|  | 4541 | std::swap(Op0, Op1);                   // Change icmp gt -> icmp lt | 
| Chris Lattner | 4456da6 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 4542 | // FALL THROUGH | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4543 | case ICmpInst::ICMP_ULT: | 
|  | 4544 | case ICmpInst::ICMP_SLT: {               // icmp lt bool A, B -> ~X & Y | 
| Chris Lattner | 4456da6 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 4545 | Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp"); | 
|  | 4546 | InsertNewInstBefore(Not, I); | 
|  | 4547 | return BinaryOperator::createAnd(Not, Op1); | 
|  | 4548 | } | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4549 | case ICmpInst::ICMP_UGE: | 
|  | 4550 | case ICmpInst::ICMP_SGE: | 
|  | 4551 | std::swap(Op0, Op1);                   // Change icmp ge -> icmp le | 
| Chris Lattner | 4456da6 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 4552 | // FALL THROUGH | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4553 | case ICmpInst::ICMP_ULE: | 
|  | 4554 | case ICmpInst::ICMP_SLE: {               //  icmp le bool %A, %B -> ~A | B | 
| Chris Lattner | 4456da6 | 2004-08-11 00:50:51 +0000 | [diff] [blame] | 4555 | Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp"); | 
|  | 4556 | InsertNewInstBefore(Not, I); | 
|  | 4557 | return BinaryOperator::createOr(Not, Op1); | 
|  | 4558 | } | 
|  | 4559 | } | 
| Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4560 | } | 
|  | 4561 |  | 
| Chris Lattner | 2dd0174 | 2004-06-09 04:24:29 +0000 | [diff] [blame] | 4562 | // See if we are doing a comparison between a constant and an instruction that | 
|  | 4563 | // can be folded into the comparison. | 
| Chris Lattner | 6d14f2a | 2002-08-09 23:47:40 +0000 | [diff] [blame] | 4564 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4565 | switch (I.getPredicate()) { | 
|  | 4566 | default: break; | 
|  | 4567 | case ICmpInst::ICMP_ULT:                        // A <u MIN -> FALSE | 
|  | 4568 | if (CI->isMinValue(false)) | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4569 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4570 | if (CI->isMaxValue(false))                    // A <u MAX -> A != MAX | 
|  | 4571 | return new ICmpInst(ICmpInst::ICMP_NE, Op0,Op1); | 
|  | 4572 | if (isMinValuePlusOne(CI,false))              // A <u MIN+1 -> A == MIN | 
|  | 4573 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, SubOne(CI)); | 
|  | 4574 | break; | 
| Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4575 |  | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4576 | case ICmpInst::ICMP_SLT: | 
|  | 4577 | if (CI->isMinValue(true))                    // A <s MIN -> FALSE | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4578 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4579 | if (CI->isMaxValue(true))                    // A <s MAX -> A != MAX | 
|  | 4580 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); | 
|  | 4581 | if (isMinValuePlusOne(CI,true))              // A <s MIN+1 -> A == MIN | 
|  | 4582 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, SubOne(CI)); | 
|  | 4583 | break; | 
|  | 4584 |  | 
|  | 4585 | case ICmpInst::ICMP_UGT: | 
|  | 4586 | if (CI->isMaxValue(false))                  // A >u MAX -> FALSE | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4587 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4588 | if (CI->isMinValue(false))                  // A >u MIN -> A != MIN | 
|  | 4589 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); | 
|  | 4590 | if (isMaxValueMinusOne(CI, false))          // A >u MAX-1 -> A == MAX | 
|  | 4591 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, AddOne(CI)); | 
|  | 4592 | break; | 
|  | 4593 |  | 
|  | 4594 | case ICmpInst::ICMP_SGT: | 
|  | 4595 | if (CI->isMaxValue(true))                   // A >s MAX -> FALSE | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4596 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4597 | if (CI->isMinValue(true))                   // A >s MIN -> A != MIN | 
|  | 4598 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); | 
|  | 4599 | if (isMaxValueMinusOne(CI, true))           // A >s MAX-1 -> A == MAX | 
|  | 4600 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, AddOne(CI)); | 
|  | 4601 | break; | 
|  | 4602 |  | 
|  | 4603 | case ICmpInst::ICMP_ULE: | 
|  | 4604 | if (CI->isMaxValue(false))                 // A <=u MAX -> TRUE | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4605 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4606 | if (CI->isMinValue(false))                 // A <=u MIN -> A == MIN | 
|  | 4607 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1); | 
|  | 4608 | if (isMaxValueMinusOne(CI,false))          // A <=u MAX-1 -> A != MAX | 
|  | 4609 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, AddOne(CI)); | 
|  | 4610 | break; | 
| Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4611 |  | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4612 | case ICmpInst::ICMP_SLE: | 
|  | 4613 | if (CI->isMaxValue(true))                  // A <=s MAX -> TRUE | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4614 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4615 | if (CI->isMinValue(true))                  // A <=s MIN -> A == MIN | 
|  | 4616 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1); | 
|  | 4617 | if (isMaxValueMinusOne(CI,true))           // A <=s MAX-1 -> A != MAX | 
|  | 4618 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, AddOne(CI)); | 
|  | 4619 | break; | 
| Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4620 |  | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4621 | case ICmpInst::ICMP_UGE: | 
|  | 4622 | if (CI->isMinValue(false))                 // A >=u MIN -> TRUE | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4623 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4624 | if (CI->isMaxValue(false))                 // A >=u MAX -> A == MAX | 
|  | 4625 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1); | 
|  | 4626 | if (isMinValuePlusOne(CI,false))           // A >=u MIN-1 -> A != MIN | 
|  | 4627 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, SubOne(CI)); | 
|  | 4628 | break; | 
|  | 4629 |  | 
|  | 4630 | case ICmpInst::ICMP_SGE: | 
|  | 4631 | if (CI->isMinValue(true))                  // A >=s MIN -> TRUE | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4632 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4633 | if (CI->isMaxValue(true))                  // A >=s MAX -> A == MAX | 
|  | 4634 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1); | 
|  | 4635 | if (isMinValuePlusOne(CI,true))            // A >=s MIN-1 -> A != MIN | 
|  | 4636 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, SubOne(CI)); | 
|  | 4637 | break; | 
| Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4638 | } | 
|  | 4639 |  | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4640 | // If we still have a icmp le or icmp ge instruction, turn it into the | 
|  | 4641 | // appropriate icmp lt or icmp gt instruction.  Since the border cases have | 
| Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 4642 | // already been handled above, this requires little checking. | 
|  | 4643 | // | 
| Reid Spencer | 624766f | 2007-03-25 19:55:33 +0000 | [diff] [blame] | 4644 | switch (I.getPredicate()) { | 
|  | 4645 | default: break; | 
|  | 4646 | case ICmpInst::ICMP_ULE: | 
|  | 4647 | return new ICmpInst(ICmpInst::ICMP_ULT, Op0, AddOne(CI)); | 
|  | 4648 | case ICmpInst::ICMP_SLE: | 
|  | 4649 | return new ICmpInst(ICmpInst::ICMP_SLT, Op0, AddOne(CI)); | 
|  | 4650 | case ICmpInst::ICMP_UGE: | 
|  | 4651 | return new ICmpInst( ICmpInst::ICMP_UGT, Op0, SubOne(CI)); | 
|  | 4652 | case ICmpInst::ICMP_SGE: | 
|  | 4653 | return new ICmpInst(ICmpInst::ICMP_SGT, Op0, SubOne(CI)); | 
|  | 4654 | } | 
| Chris Lattner | ee0f280 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 4655 |  | 
|  | 4656 | // See if we can fold the comparison based on bits known to be zero or one | 
|  | 4657 | // in the input. | 
| Reid Spencer | c3e3b8a | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 4658 | uint32_t BitWidth = cast<IntegerType>(Ty)->getBitWidth(); | 
|  | 4659 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); | 
|  | 4660 | if (SimplifyDemandedBits(Op0, APInt::getAllOnesValue(BitWidth), | 
| Chris Lattner | ee0f280 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 4661 | KnownZero, KnownOne, 0)) | 
|  | 4662 | return &I; | 
|  | 4663 |  | 
|  | 4664 | // Given the known and unknown bits, compute a range that the LHS could be | 
|  | 4665 | // in. | 
| Reid Spencer | c3e3b8a | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 4666 | if ((KnownOne | KnownZero) != 0) { | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4667 | // Compute the Min, Max and RHS values based on the known bits. For the | 
|  | 4668 | // EQ and NE we use unsigned values. | 
| Zhou Sheng | 150f3bb | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 4669 | APInt Min(BitWidth, 0), Max(BitWidth, 0); | 
|  | 4670 | const APInt& RHSVal = CI->getValue(); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4671 | if (ICmpInst::isSignedPredicate(I.getPredicate())) { | 
| Reid Spencer | c3e3b8a | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 4672 | ComputeSignedMinMaxValuesFromKnownBits(Ty, KnownZero, KnownOne, Min, | 
|  | 4673 | Max); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4674 | } else { | 
| Reid Spencer | c3e3b8a | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 4675 | ComputeUnsignedMinMaxValuesFromKnownBits(Ty, KnownZero, KnownOne, Min, | 
|  | 4676 | Max); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4677 | } | 
|  | 4678 | switch (I.getPredicate()) {  // LE/GE have been folded already. | 
|  | 4679 | default: assert(0 && "Unknown icmp opcode!"); | 
|  | 4680 | case ICmpInst::ICMP_EQ: | 
| Reid Spencer | c3e3b8a | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 4681 | if (Max.ult(RHSVal) || Min.ugt(RHSVal)) | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4682 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4683 | break; | 
|  | 4684 | case ICmpInst::ICMP_NE: | 
| Reid Spencer | c3e3b8a | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 4685 | if (Max.ult(RHSVal) || Min.ugt(RHSVal)) | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4686 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4687 | break; | 
|  | 4688 | case ICmpInst::ICMP_ULT: | 
| Reid Spencer | c3e3b8a | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 4689 | if (Max.ult(RHSVal)) | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4690 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); | 
| Reid Spencer | c3e3b8a | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 4691 | if (Min.ugt(RHSVal)) | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4692 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4693 | break; | 
|  | 4694 | case ICmpInst::ICMP_UGT: | 
| Reid Spencer | c3e3b8a | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 4695 | if (Min.ugt(RHSVal)) | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4696 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); | 
| Reid Spencer | c3e3b8a | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 4697 | if (Max.ult(RHSVal)) | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4698 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4699 | break; | 
|  | 4700 | case ICmpInst::ICMP_SLT: | 
| Reid Spencer | c3e3b8a | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 4701 | if (Max.slt(RHSVal)) | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4702 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); | 
| Reid Spencer | c3e3b8a | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 4703 | if (Min.sgt(RHSVal)) | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4704 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4705 | break; | 
|  | 4706 | case ICmpInst::ICMP_SGT: | 
| Reid Spencer | c3e3b8a | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 4707 | if (Min.sgt(RHSVal)) | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4708 | return ReplaceInstUsesWith(I, ConstantInt::getTrue()); | 
| Reid Spencer | c3e3b8a | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 4709 | if (Max.slt(RHSVal)) | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4710 | return ReplaceInstUsesWith(I, ConstantInt::getFalse()); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4711 | break; | 
| Chris Lattner | ee0f280 | 2006-02-12 02:07:56 +0000 | [diff] [blame] | 4712 | } | 
|  | 4713 | } | 
|  | 4714 |  | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4715 | // Since the RHS is a ConstantInt (CI), if the left hand side is an | 
| Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 4716 | // instruction, see if that instruction also has constants so that the | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4717 | // instruction can be folded into the icmp | 
| Chris Lattner | e1e10e1 | 2004-05-25 06:32:08 +0000 | [diff] [blame] | 4718 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) | 
| Chris Lattner | a74deaf | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 4719 | if (Instruction *Res = visitICmpInstWithInstAndIntCst(I, LHSI, CI)) | 
|  | 4720 | return Res; | 
| Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4721 | } | 
|  | 4722 |  | 
| Chris Lattner | a74deaf | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 4723 | // Handle icmp with constant (but not simple integer constant) RHS | 
| Chris Lattner | 77c32c3 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 4724 | if (Constant *RHSC = dyn_cast<Constant>(Op1)) { | 
|  | 4725 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) | 
|  | 4726 | switch (LHSI->getOpcode()) { | 
| Chris Lattner | a816eee | 2005-05-01 04:42:15 +0000 | [diff] [blame] | 4727 | case Instruction::GetElementPtr: | 
|  | 4728 | if (RHSC->isNullValue()) { | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4729 | // icmp pred GEP (P, int 0, int 0, int 0), null -> icmp pred P, null | 
| Chris Lattner | a816eee | 2005-05-01 04:42:15 +0000 | [diff] [blame] | 4730 | bool isAllZeros = true; | 
|  | 4731 | for (unsigned i = 1, e = LHSI->getNumOperands(); i != e; ++i) | 
|  | 4732 | if (!isa<Constant>(LHSI->getOperand(i)) || | 
|  | 4733 | !cast<Constant>(LHSI->getOperand(i))->isNullValue()) { | 
|  | 4734 | isAllZeros = false; | 
|  | 4735 | break; | 
|  | 4736 | } | 
|  | 4737 | if (isAllZeros) | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4738 | return new ICmpInst(I.getPredicate(), LHSI->getOperand(0), | 
| Chris Lattner | a816eee | 2005-05-01 04:42:15 +0000 | [diff] [blame] | 4739 | Constant::getNullValue(LHSI->getOperand(0)->getType())); | 
|  | 4740 | } | 
|  | 4741 | break; | 
|  | 4742 |  | 
| Chris Lattner | 77c32c3 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 4743 | case Instruction::PHI: | 
|  | 4744 | if (Instruction *NV = FoldOpIntoPhi(I)) | 
|  | 4745 | return NV; | 
|  | 4746 | break; | 
| Chris Lattner | 3dbe65f | 2007-04-06 18:57:34 +0000 | [diff] [blame] | 4747 | case Instruction::Select: { | 
| Chris Lattner | 77c32c3 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 4748 | // If either operand of the select is a constant, we can fold the | 
|  | 4749 | // comparison into the select arms, which will cause one to be | 
|  | 4750 | // constant folded and the select turned into a bitwise or. | 
|  | 4751 | Value *Op1 = 0, *Op2 = 0; | 
|  | 4752 | if (LHSI->hasOneUse()) { | 
|  | 4753 | if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) { | 
|  | 4754 | // Fold the known value into the constant operand. | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4755 | Op1 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC); | 
|  | 4756 | // Insert a new ICmp of the other select operand. | 
|  | 4757 | Op2 = InsertNewInstBefore(new ICmpInst(I.getPredicate(), | 
|  | 4758 | LHSI->getOperand(2), RHSC, | 
|  | 4759 | I.getName()), I); | 
| Chris Lattner | 77c32c3 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 4760 | } else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) { | 
|  | 4761 | // Fold the known value into the constant operand. | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4762 | Op2 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC); | 
|  | 4763 | // Insert a new ICmp of the other select operand. | 
|  | 4764 | Op1 = InsertNewInstBefore(new ICmpInst(I.getPredicate(), | 
|  | 4765 | LHSI->getOperand(1), RHSC, | 
|  | 4766 | I.getName()), I); | 
| Chris Lattner | 77c32c3 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 4767 | } | 
|  | 4768 | } | 
| Jeff Cohen | 8263985 | 2005-04-23 21:38:35 +0000 | [diff] [blame] | 4769 |  | 
| Chris Lattner | 77c32c3 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 4770 | if (Op1) | 
|  | 4771 | return new SelectInst(LHSI->getOperand(0), Op1, Op2); | 
|  | 4772 | break; | 
|  | 4773 | } | 
| Chris Lattner | 3dbe65f | 2007-04-06 18:57:34 +0000 | [diff] [blame] | 4774 | case Instruction::Malloc: | 
|  | 4775 | // If we have (malloc != null), and if the malloc has a single use, we | 
|  | 4776 | // can assume it is successful and remove the malloc. | 
|  | 4777 | if (LHSI->hasOneUse() && isa<ConstantPointerNull>(RHSC)) { | 
|  | 4778 | AddToWorkList(LHSI); | 
|  | 4779 | return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty, | 
|  | 4780 | !isTrueWhenEqual(I))); | 
|  | 4781 | } | 
|  | 4782 | break; | 
|  | 4783 | } | 
| Chris Lattner | 77c32c3 | 2005-04-23 15:31:55 +0000 | [diff] [blame] | 4784 | } | 
|  | 4785 |  | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4786 | // If we can optimize a 'icmp GEP, P' or 'icmp P, GEP', do so now. | 
| Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4787 | if (User *GEP = dyn_castGetElementPtr(Op0)) | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4788 | if (Instruction *NI = FoldGEPICmp(GEP, Op1, I.getPredicate(), I)) | 
| Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4789 | return NI; | 
|  | 4790 | if (User *GEP = dyn_castGetElementPtr(Op1)) | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4791 | if (Instruction *NI = FoldGEPICmp(GEP, Op0, | 
|  | 4792 | ICmpInst::getSwappedPredicate(I.getPredicate()), I)) | 
| Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 4793 | return NI; | 
|  | 4794 |  | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4795 | // Test to see if the operands of the icmp are casted versions of other | 
| Chris Lattner | 64d87b0 | 2007-01-06 01:45:59 +0000 | [diff] [blame] | 4796 | // values.  If the ptr->ptr cast can be stripped off both arguments, we do so | 
|  | 4797 | // now. | 
|  | 4798 | if (BitCastInst *CI = dyn_cast<BitCastInst>(Op0)) { | 
|  | 4799 | if (isa<PointerType>(Op0->getType()) && | 
|  | 4800 | (isa<Constant>(Op1) || isa<BitCastInst>(Op1))) { | 
| Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 4801 | // We keep moving the cast from the left operand over to the right | 
|  | 4802 | // operand, where it can often be eliminated completely. | 
| Chris Lattner | 64d87b0 | 2007-01-06 01:45:59 +0000 | [diff] [blame] | 4803 | Op0 = CI->getOperand(0); | 
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4804 |  | 
| Chris Lattner | 64d87b0 | 2007-01-06 01:45:59 +0000 | [diff] [blame] | 4805 | // If operand #1 is a bitcast instruction, it must also be a ptr->ptr cast | 
|  | 4806 | // so eliminate it as well. | 
|  | 4807 | if (BitCastInst *CI2 = dyn_cast<BitCastInst>(Op1)) | 
|  | 4808 | Op1 = CI2->getOperand(0); | 
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 4809 |  | 
| Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 4810 | // If Op1 is a constant, we can fold the cast into the constant. | 
| Chris Lattner | 64d87b0 | 2007-01-06 01:45:59 +0000 | [diff] [blame] | 4811 | if (Op0->getType() != Op1->getType()) | 
| Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 4812 | if (Constant *Op1C = dyn_cast<Constant>(Op1)) { | 
| Reid Spencer | bb65ebf | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 4813 | Op1 = ConstantExpr::getBitCast(Op1C, Op0->getType()); | 
| Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 4814 | } else { | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4815 | // Otherwise, cast the RHS right before the icmp | 
| Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 4816 | Op1 = InsertCastBefore(Instruction::BitCast, Op1, Op0->getType(), I); | 
| Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 4817 | } | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4818 | return new ICmpInst(I.getPredicate(), Op0, Op1); | 
| Chris Lattner | 1693079 | 2003-11-03 04:25:02 +0000 | [diff] [blame] | 4819 | } | 
| Chris Lattner | 64d87b0 | 2007-01-06 01:45:59 +0000 | [diff] [blame] | 4820 | } | 
|  | 4821 |  | 
|  | 4822 | if (isa<CastInst>(Op0)) { | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4823 | // Handle the special case of: icmp (cast bool to X), <cst> | 
| Chris Lattner | 6444c37 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 4824 | // This comes up when you have code like | 
|  | 4825 | //   int X = A < B; | 
|  | 4826 | //   if (X) ... | 
|  | 4827 | // For generality, we handle any zero-extension of any operand comparison | 
| Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 4828 | // with a constant or another cast from the same type. | 
|  | 4829 | if (isa<ConstantInt>(Op1) || isa<CastInst>(Op1)) | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4830 | if (Instruction *R = visitICmpInstWithCastAndCast(I)) | 
| Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 4831 | return R; | 
| Chris Lattner | 6444c37 | 2003-11-03 05:17:03 +0000 | [diff] [blame] | 4832 | } | 
| Chris Lattner | f5c8a0b | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 4833 |  | 
| Chris Lattner | b3f24c9 | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 4834 | if (I.isEquality()) { | 
| Chris Lattner | 17c7c03 | 2007-01-05 03:04:57 +0000 | [diff] [blame] | 4835 | Value *A, *B, *C, *D; | 
|  | 4836 | if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) { | 
|  | 4837 | if (A == Op1 || B == Op1) {    // (A^B) == A  ->  B == 0 | 
|  | 4838 | Value *OtherVal = A == Op1 ? B : A; | 
|  | 4839 | return new ICmpInst(I.getPredicate(), OtherVal, | 
|  | 4840 | Constant::getNullValue(A->getType())); | 
|  | 4841 | } | 
|  | 4842 |  | 
|  | 4843 | if (match(Op1, m_Xor(m_Value(C), m_Value(D)))) { | 
|  | 4844 | // A^c1 == C^c2 --> A == C^(c1^c2) | 
|  | 4845 | if (ConstantInt *C1 = dyn_cast<ConstantInt>(B)) | 
|  | 4846 | if (ConstantInt *C2 = dyn_cast<ConstantInt>(D)) | 
|  | 4847 | if (Op1->hasOneUse()) { | 
| Zhou Sheng | 9bc8ab1 | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 4848 | Constant *NC = ConstantInt::get(C1->getValue() ^ C2->getValue()); | 
| Chris Lattner | 17c7c03 | 2007-01-05 03:04:57 +0000 | [diff] [blame] | 4849 | Instruction *Xor = BinaryOperator::createXor(C, NC, "tmp"); | 
|  | 4850 | return new ICmpInst(I.getPredicate(), A, | 
|  | 4851 | InsertNewInstBefore(Xor, I)); | 
|  | 4852 | } | 
|  | 4853 |  | 
|  | 4854 | // A^B == A^D -> B == D | 
|  | 4855 | if (A == C) return new ICmpInst(I.getPredicate(), B, D); | 
|  | 4856 | if (A == D) return new ICmpInst(I.getPredicate(), B, C); | 
|  | 4857 | if (B == C) return new ICmpInst(I.getPredicate(), A, D); | 
|  | 4858 | if (B == D) return new ICmpInst(I.getPredicate(), A, C); | 
|  | 4859 | } | 
|  | 4860 | } | 
|  | 4861 |  | 
|  | 4862 | if (match(Op1, m_Xor(m_Value(A), m_Value(B))) && | 
|  | 4863 | (A == Op0 || B == Op0)) { | 
| Chris Lattner | f5c8a0b | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 4864 | // A == (A^B)  ->  B == 0 | 
|  | 4865 | Value *OtherVal = A == Op0 ? B : A; | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4866 | return new ICmpInst(I.getPredicate(), OtherVal, | 
|  | 4867 | Constant::getNullValue(A->getType())); | 
| Chris Lattner | 17c7c03 | 2007-01-05 03:04:57 +0000 | [diff] [blame] | 4868 | } | 
|  | 4869 | if (match(Op0, m_Sub(m_Value(A), m_Value(B))) && A == Op1) { | 
| Chris Lattner | f5c8a0b | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 4870 | // (A-B) == A  ->  B == 0 | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4871 | return new ICmpInst(I.getPredicate(), B, | 
|  | 4872 | Constant::getNullValue(B->getType())); | 
| Chris Lattner | 17c7c03 | 2007-01-05 03:04:57 +0000 | [diff] [blame] | 4873 | } | 
|  | 4874 | if (match(Op1, m_Sub(m_Value(A), m_Value(B))) && A == Op0) { | 
| Chris Lattner | f5c8a0b | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 4875 | // A == (A-B)  ->  B == 0 | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4876 | return new ICmpInst(I.getPredicate(), B, | 
|  | 4877 | Constant::getNullValue(B->getType())); | 
| Chris Lattner | f5c8a0b | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 4878 | } | 
| Chris Lattner | d12a4bf | 2006-11-14 06:06:06 +0000 | [diff] [blame] | 4879 |  | 
| Chris Lattner | d12a4bf | 2006-11-14 06:06:06 +0000 | [diff] [blame] | 4880 | // (X&Z) == (Y&Z) -> (X^Y) & Z == 0 | 
|  | 4881 | if (Op0->hasOneUse() && Op1->hasOneUse() && | 
|  | 4882 | match(Op0, m_And(m_Value(A), m_Value(B))) && | 
|  | 4883 | match(Op1, m_And(m_Value(C), m_Value(D)))) { | 
|  | 4884 | Value *X = 0, *Y = 0, *Z = 0; | 
|  | 4885 |  | 
|  | 4886 | if (A == C) { | 
|  | 4887 | X = B; Y = D; Z = A; | 
|  | 4888 | } else if (A == D) { | 
|  | 4889 | X = B; Y = C; Z = A; | 
|  | 4890 | } else if (B == C) { | 
|  | 4891 | X = A; Y = D; Z = B; | 
|  | 4892 | } else if (B == D) { | 
|  | 4893 | X = A; Y = C; Z = B; | 
|  | 4894 | } | 
|  | 4895 |  | 
|  | 4896 | if (X) {   // Build (X^Y) & Z | 
|  | 4897 | Op1 = InsertNewInstBefore(BinaryOperator::createXor(X, Y, "tmp"), I); | 
|  | 4898 | Op1 = InsertNewInstBefore(BinaryOperator::createAnd(Op1, Z, "tmp"), I); | 
|  | 4899 | I.setOperand(0, Op1); | 
|  | 4900 | I.setOperand(1, Constant::getNullValue(Op1->getType())); | 
|  | 4901 | return &I; | 
|  | 4902 | } | 
|  | 4903 | } | 
| Chris Lattner | f5c8a0b | 2006-02-27 01:44:11 +0000 | [diff] [blame] | 4904 | } | 
| Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 4905 | return Changed ? &I : 0; | 
| Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 4906 | } | 
|  | 4907 |  | 
| Chris Lattner | a74deaf | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 4908 | /// visitICmpInstWithInstAndIntCst - Handle "icmp (instr, intcst)". | 
|  | 4909 | /// | 
|  | 4910 | Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI, | 
|  | 4911 | Instruction *LHSI, | 
|  | 4912 | ConstantInt *RHS) { | 
|  | 4913 | const APInt &RHSV = RHS->getValue(); | 
|  | 4914 |  | 
|  | 4915 | switch (LHSI->getOpcode()) { | 
| Duncan Sands | f01a47c | 2007-04-04 06:42:45 +0000 | [diff] [blame] | 4916 | case Instruction::Xor:         // (icmp pred (xor X, XorCST), CI) | 
| Chris Lattner | a74deaf | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 4917 | if (ConstantInt *XorCST = dyn_cast<ConstantInt>(LHSI->getOperand(1))) { | 
|  | 4918 | // If this is a comparison that tests the signbit (X < 0) or (x > -1), | 
|  | 4919 | // fold the xor. | 
|  | 4920 | if (ICI.getPredicate() == ICmpInst::ICMP_SLT && RHSV == 0 || | 
|  | 4921 | ICI.getPredicate() == ICmpInst::ICMP_SGT && RHSV.isAllOnesValue()) { | 
|  | 4922 | Value *CompareVal = LHSI->getOperand(0); | 
|  | 4923 |  | 
|  | 4924 | // If the sign bit of the XorCST is not set, there is no change to | 
|  | 4925 | // the operation, just stop using the Xor. | 
|  | 4926 | if (!XorCST->getValue().isNegative()) { | 
|  | 4927 | ICI.setOperand(0, CompareVal); | 
|  | 4928 | AddToWorkList(LHSI); | 
|  | 4929 | return &ICI; | 
|  | 4930 | } | 
|  | 4931 |  | 
|  | 4932 | // Was the old condition true if the operand is positive? | 
|  | 4933 | bool isTrueIfPositive = ICI.getPredicate() == ICmpInst::ICMP_SGT; | 
|  | 4934 |  | 
|  | 4935 | // If so, the new one isn't. | 
|  | 4936 | isTrueIfPositive ^= true; | 
|  | 4937 |  | 
|  | 4938 | if (isTrueIfPositive) | 
|  | 4939 | return new ICmpInst(ICmpInst::ICMP_SGT, CompareVal, SubOne(RHS)); | 
|  | 4940 | else | 
|  | 4941 | return new ICmpInst(ICmpInst::ICMP_SLT, CompareVal, AddOne(RHS)); | 
|  | 4942 | } | 
|  | 4943 | } | 
|  | 4944 | break; | 
|  | 4945 | case Instruction::And:         // (icmp pred (and X, AndCST), RHS) | 
|  | 4946 | if (LHSI->hasOneUse() && isa<ConstantInt>(LHSI->getOperand(1)) && | 
|  | 4947 | LHSI->getOperand(0)->hasOneUse()) { | 
|  | 4948 | ConstantInt *AndCST = cast<ConstantInt>(LHSI->getOperand(1)); | 
|  | 4949 |  | 
|  | 4950 | // If the LHS is an AND of a truncating cast, we can widen the | 
|  | 4951 | // and/compare to be the input width without changing the value | 
|  | 4952 | // produced, eliminating a cast. | 
|  | 4953 | if (TruncInst *Cast = dyn_cast<TruncInst>(LHSI->getOperand(0))) { | 
|  | 4954 | // We can do this transformation if either the AND constant does not | 
|  | 4955 | // have its sign bit set or if it is an equality comparison. | 
|  | 4956 | // Extending a relational comparison when we're checking the sign | 
|  | 4957 | // bit would not work. | 
|  | 4958 | if (Cast->hasOneUse() && | 
|  | 4959 | (ICI.isEquality() || AndCST->getValue().isPositive() && | 
|  | 4960 | RHSV.isPositive())) { | 
|  | 4961 | uint32_t BitWidth = | 
|  | 4962 | cast<IntegerType>(Cast->getOperand(0)->getType())->getBitWidth(); | 
|  | 4963 | APInt NewCST = AndCST->getValue(); | 
|  | 4964 | NewCST.zext(BitWidth); | 
|  | 4965 | APInt NewCI = RHSV; | 
|  | 4966 | NewCI.zext(BitWidth); | 
|  | 4967 | Instruction *NewAnd = | 
|  | 4968 | BinaryOperator::createAnd(Cast->getOperand(0), | 
|  | 4969 | ConstantInt::get(NewCST),LHSI->getName()); | 
|  | 4970 | InsertNewInstBefore(NewAnd, ICI); | 
|  | 4971 | return new ICmpInst(ICI.getPredicate(), NewAnd, | 
|  | 4972 | ConstantInt::get(NewCI)); | 
|  | 4973 | } | 
|  | 4974 | } | 
|  | 4975 |  | 
|  | 4976 | // If this is: (X >> C1) & C2 != C3 (where any shift and any compare | 
|  | 4977 | // could exist), turn it into (X & (C2 << C1)) != (C3 << C1).  This | 
|  | 4978 | // happens a LOT in code produced by the C front-end, for bitfield | 
|  | 4979 | // access. | 
|  | 4980 | BinaryOperator *Shift = dyn_cast<BinaryOperator>(LHSI->getOperand(0)); | 
|  | 4981 | if (Shift && !Shift->isShift()) | 
|  | 4982 | Shift = 0; | 
|  | 4983 |  | 
|  | 4984 | ConstantInt *ShAmt; | 
|  | 4985 | ShAmt = Shift ? dyn_cast<ConstantInt>(Shift->getOperand(1)) : 0; | 
|  | 4986 | const Type *Ty = Shift ? Shift->getType() : 0;  // Type of the shift. | 
|  | 4987 | const Type *AndTy = AndCST->getType();          // Type of the and. | 
|  | 4988 |  | 
|  | 4989 | // We can fold this as long as we can't shift unknown bits | 
|  | 4990 | // into the mask.  This can only happen with signed shift | 
|  | 4991 | // rights, as they sign-extend. | 
|  | 4992 | if (ShAmt) { | 
|  | 4993 | bool CanFold = Shift->isLogicalShift(); | 
|  | 4994 | if (!CanFold) { | 
|  | 4995 | // To test for the bad case of the signed shr, see if any | 
|  | 4996 | // of the bits shifted in could be tested after the mask. | 
|  | 4997 | uint32_t TyBits = Ty->getPrimitiveSizeInBits(); | 
|  | 4998 | int ShAmtVal = TyBits - ShAmt->getLimitedValue(TyBits); | 
|  | 4999 |  | 
|  | 5000 | uint32_t BitWidth = AndTy->getPrimitiveSizeInBits(); | 
|  | 5001 | if ((APInt::getHighBitsSet(BitWidth, BitWidth-ShAmtVal) & | 
|  | 5002 | AndCST->getValue()) == 0) | 
|  | 5003 | CanFold = true; | 
|  | 5004 | } | 
|  | 5005 |  | 
|  | 5006 | if (CanFold) { | 
|  | 5007 | Constant *NewCst; | 
|  | 5008 | if (Shift->getOpcode() == Instruction::Shl) | 
|  | 5009 | NewCst = ConstantExpr::getLShr(RHS, ShAmt); | 
|  | 5010 | else | 
|  | 5011 | NewCst = ConstantExpr::getShl(RHS, ShAmt); | 
|  | 5012 |  | 
|  | 5013 | // Check to see if we are shifting out any of the bits being | 
|  | 5014 | // compared. | 
|  | 5015 | if (ConstantExpr::get(Shift->getOpcode(), NewCst, ShAmt) != RHS) { | 
|  | 5016 | // If we shifted bits out, the fold is not going to work out. | 
|  | 5017 | // As a special case, check to see if this means that the | 
|  | 5018 | // result is always true or false now. | 
|  | 5019 | if (ICI.getPredicate() == ICmpInst::ICMP_EQ) | 
|  | 5020 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); | 
|  | 5021 | if (ICI.getPredicate() == ICmpInst::ICMP_NE) | 
|  | 5022 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue()); | 
|  | 5023 | } else { | 
|  | 5024 | ICI.setOperand(1, NewCst); | 
|  | 5025 | Constant *NewAndCST; | 
|  | 5026 | if (Shift->getOpcode() == Instruction::Shl) | 
|  | 5027 | NewAndCST = ConstantExpr::getLShr(AndCST, ShAmt); | 
|  | 5028 | else | 
|  | 5029 | NewAndCST = ConstantExpr::getShl(AndCST, ShAmt); | 
|  | 5030 | LHSI->setOperand(1, NewAndCST); | 
|  | 5031 | LHSI->setOperand(0, Shift->getOperand(0)); | 
|  | 5032 | AddToWorkList(Shift); // Shift is dead. | 
|  | 5033 | AddUsesToWorkList(ICI); | 
|  | 5034 | return &ICI; | 
|  | 5035 | } | 
|  | 5036 | } | 
|  | 5037 | } | 
|  | 5038 |  | 
|  | 5039 | // Turn ((X >> Y) & C) == 0  into  (X & (C << Y)) == 0.  The later is | 
|  | 5040 | // preferable because it allows the C<<Y expression to be hoisted out | 
|  | 5041 | // of a loop if Y is invariant and X is not. | 
|  | 5042 | if (Shift && Shift->hasOneUse() && RHSV == 0 && | 
|  | 5043 | ICI.isEquality() && !Shift->isArithmeticShift() && | 
|  | 5044 | isa<Instruction>(Shift->getOperand(0))) { | 
|  | 5045 | // Compute C << Y. | 
|  | 5046 | Value *NS; | 
|  | 5047 | if (Shift->getOpcode() == Instruction::LShr) { | 
|  | 5048 | NS = BinaryOperator::createShl(AndCST, | 
|  | 5049 | Shift->getOperand(1), "tmp"); | 
|  | 5050 | } else { | 
|  | 5051 | // Insert a logical shift. | 
|  | 5052 | NS = BinaryOperator::createLShr(AndCST, | 
|  | 5053 | Shift->getOperand(1), "tmp"); | 
|  | 5054 | } | 
|  | 5055 | InsertNewInstBefore(cast<Instruction>(NS), ICI); | 
|  | 5056 |  | 
|  | 5057 | // Compute X & (C << Y). | 
|  | 5058 | Instruction *NewAnd = | 
|  | 5059 | BinaryOperator::createAnd(Shift->getOperand(0), NS, LHSI->getName()); | 
|  | 5060 | InsertNewInstBefore(NewAnd, ICI); | 
|  | 5061 |  | 
|  | 5062 | ICI.setOperand(0, NewAnd); | 
|  | 5063 | return &ICI; | 
|  | 5064 | } | 
|  | 5065 | } | 
|  | 5066 | break; | 
|  | 5067 |  | 
|  | 5068 | case Instruction::Shl:         // (icmp pred (shl X, ShAmt), CI) | 
|  | 5069 | if (ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI->getOperand(1))) { | 
|  | 5070 | if (ICI.isEquality()) { | 
|  | 5071 | uint32_t TypeBits = RHSV.getBitWidth(); | 
|  | 5072 |  | 
|  | 5073 | // Check that the shift amount is in range.  If not, don't perform | 
|  | 5074 | // undefined shifts.  When the shift is visited it will be | 
|  | 5075 | // simplified. | 
|  | 5076 | if (ShAmt->uge(TypeBits)) | 
|  | 5077 | break; | 
|  | 5078 |  | 
|  | 5079 | // If we are comparing against bits always shifted out, the | 
|  | 5080 | // comparison cannot succeed. | 
|  | 5081 | Constant *Comp = | 
|  | 5082 | ConstantExpr::getShl(ConstantExpr::getLShr(RHS, ShAmt), ShAmt); | 
|  | 5083 | if (Comp != RHS) {// Comparing against a bit that we know is zero. | 
|  | 5084 | bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE; | 
|  | 5085 | Constant *Cst = ConstantInt::get(Type::Int1Ty, IsICMP_NE); | 
|  | 5086 | return ReplaceInstUsesWith(ICI, Cst); | 
|  | 5087 | } | 
|  | 5088 |  | 
|  | 5089 | if (LHSI->hasOneUse()) { | 
|  | 5090 | // Otherwise strength reduce the shift into an and. | 
|  | 5091 | uint32_t ShAmtVal = (uint32_t)ShAmt->getLimitedValue(TypeBits); | 
|  | 5092 | Constant *Mask = | 
|  | 5093 | ConstantInt::get(APInt::getLowBitsSet(TypeBits, TypeBits-ShAmtVal)); | 
|  | 5094 |  | 
|  | 5095 | Instruction *AndI = | 
|  | 5096 | BinaryOperator::createAnd(LHSI->getOperand(0), | 
|  | 5097 | Mask, LHSI->getName()+".mask"); | 
|  | 5098 | Value *And = InsertNewInstBefore(AndI, ICI); | 
|  | 5099 | return new ICmpInst(ICI.getPredicate(), And, | 
| Chris Lattner | e5bbb3c | 2007-04-03 23:29:39 +0000 | [diff] [blame] | 5100 | ConstantInt::get(RHSV.lshr(ShAmtVal))); | 
| Chris Lattner | a74deaf | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 5101 | } | 
|  | 5102 | } | 
|  | 5103 | } | 
|  | 5104 | break; | 
|  | 5105 |  | 
|  | 5106 | case Instruction::LShr:         // (icmp pred (shr X, ShAmt), CI) | 
|  | 5107 | case Instruction::AShr: | 
|  | 5108 | if (ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI->getOperand(1))) { | 
|  | 5109 | if (ICI.isEquality()) { | 
|  | 5110 | // Check that the shift amount is in range.  If not, don't perform | 
|  | 5111 | // undefined shifts.  When the shift is visited it will be | 
|  | 5112 | // simplified. | 
|  | 5113 | uint32_t TypeBits = RHSV.getBitWidth(); | 
|  | 5114 | if (ShAmt->uge(TypeBits)) | 
|  | 5115 | break; | 
|  | 5116 | uint32_t ShAmtVal = (uint32_t)ShAmt->getLimitedValue(TypeBits); | 
|  | 5117 |  | 
|  | 5118 | // If we are comparing against bits always shifted out, the | 
|  | 5119 | // comparison cannot succeed. | 
|  | 5120 | APInt Comp = RHSV << ShAmtVal; | 
|  | 5121 | if (LHSI->getOpcode() == Instruction::LShr) | 
|  | 5122 | Comp = Comp.lshr(ShAmtVal); | 
|  | 5123 | else | 
|  | 5124 | Comp = Comp.ashr(ShAmtVal); | 
|  | 5125 |  | 
|  | 5126 | if (Comp != RHSV) { // Comparing against a bit that we know is zero. | 
|  | 5127 | bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE; | 
|  | 5128 | Constant *Cst = ConstantInt::get(Type::Int1Ty, IsICMP_NE); | 
|  | 5129 | return ReplaceInstUsesWith(ICI, Cst); | 
|  | 5130 | } | 
|  | 5131 |  | 
|  | 5132 | if (LHSI->hasOneUse() || RHSV == 0) { | 
|  | 5133 | // Otherwise strength reduce the shift into an and. | 
|  | 5134 | APInt Val(APInt::getHighBitsSet(TypeBits, TypeBits - ShAmtVal)); | 
|  | 5135 | Constant *Mask = ConstantInt::get(Val); | 
|  | 5136 |  | 
|  | 5137 | Instruction *AndI = | 
|  | 5138 | BinaryOperator::createAnd(LHSI->getOperand(0), | 
|  | 5139 | Mask, LHSI->getName()+".mask"); | 
|  | 5140 | Value *And = InsertNewInstBefore(AndI, ICI); | 
|  | 5141 | return new ICmpInst(ICI.getPredicate(), And, | 
|  | 5142 | ConstantExpr::getShl(RHS, ShAmt)); | 
|  | 5143 | } | 
|  | 5144 | } | 
|  | 5145 | } | 
|  | 5146 | break; | 
|  | 5147 |  | 
|  | 5148 | case Instruction::SDiv: | 
|  | 5149 | case Instruction::UDiv: | 
|  | 5150 | // Fold: icmp pred ([us]div X, C1), C2 -> range test | 
|  | 5151 | // Fold this div into the comparison, producing a range check. | 
|  | 5152 | // Determine, based on the divide type, what the range is being | 
|  | 5153 | // checked.  If there is an overflow on the low or high side, remember | 
|  | 5154 | // it, otherwise compute the range [low, hi) bounding the new value. | 
|  | 5155 | // See: InsertRangeTest above for the kinds of replacements possible. | 
|  | 5156 | if (ConstantInt *DivRHS = dyn_cast<ConstantInt>(LHSI->getOperand(1))) { | 
|  | 5157 | // FIXME: If the operand types don't match the type of the divide | 
|  | 5158 | // then don't attempt this transform. The code below doesn't have the | 
|  | 5159 | // logic to deal with a signed divide and an unsigned compare (and | 
|  | 5160 | // vice versa). This is because (x /s C1) <s C2  produces different | 
|  | 5161 | // results than (x /s C1) <u C2 or (x /u C1) <s C2 or even | 
|  | 5162 | // (x /u C1) <u C2.  Simply casting the operands and result won't | 
|  | 5163 | // work. :(  The if statement below tests that condition and bails | 
|  | 5164 | // if it finds it. | 
|  | 5165 | bool DivIsSigned = LHSI->getOpcode() == Instruction::SDiv; | 
|  | 5166 | if (!ICI.isEquality() && DivIsSigned != ICI.isSignedPredicate()) | 
|  | 5167 | break; | 
|  | 5168 | if (DivRHS->isZero()) | 
|  | 5169 | break; // Don't hack on div by zero | 
|  | 5170 |  | 
|  | 5171 | // Initialize the variables that will indicate the nature of the | 
|  | 5172 | // range check. | 
|  | 5173 | bool LoOverflow = false, HiOverflow = false; | 
|  | 5174 | ConstantInt *LoBound = 0, *HiBound = 0; | 
|  | 5175 |  | 
|  | 5176 | // Compute Prod = CI * DivRHS. We are essentially solving an equation | 
|  | 5177 | // of form X/C1=C2. We solve for X by multiplying C1 (DivRHS) and | 
|  | 5178 | // C2 (CI). By solving for X we can turn this into a range check | 
|  | 5179 | // instead of computing a divide. | 
|  | 5180 | ConstantInt *Prod = Multiply(RHS, DivRHS); | 
|  | 5181 |  | 
|  | 5182 | // Determine if the product overflows by seeing if the product is | 
|  | 5183 | // not equal to the divide. Make sure we do the same kind of divide | 
|  | 5184 | // as in the LHS instruction that we're folding. | 
|  | 5185 | bool ProdOV = (DivIsSigned ? ConstantExpr::getSDiv(Prod, DivRHS) : | 
|  | 5186 | ConstantExpr::getUDiv(Prod, DivRHS)) != RHS; | 
|  | 5187 |  | 
|  | 5188 | // Get the ICmp opcode | 
|  | 5189 | ICmpInst::Predicate predicate = ICI.getPredicate(); | 
|  | 5190 |  | 
|  | 5191 | if (!DivIsSigned) {  // udiv | 
|  | 5192 | LoBound = Prod; | 
|  | 5193 | LoOverflow = ProdOV; | 
|  | 5194 | HiOverflow = ProdOV || | 
|  | 5195 | AddWithOverflow(HiBound, LoBound, DivRHS, false); | 
|  | 5196 | } else if (DivRHS->getValue().isPositive()) { // Divisor is > 0. | 
|  | 5197 | if (RHSV == 0) {       // (X / pos) op 0 | 
|  | 5198 | // Can't overflow. | 
|  | 5199 | LoBound = cast<ConstantInt>(ConstantExpr::getNeg(SubOne(DivRHS))); | 
|  | 5200 | HiBound = DivRHS; | 
|  | 5201 | } else if (RHSV.isPositive()) {   // (X / pos) op pos | 
|  | 5202 | LoBound = Prod; | 
|  | 5203 | LoOverflow = ProdOV; | 
|  | 5204 | HiOverflow = ProdOV || | 
|  | 5205 | AddWithOverflow(HiBound, Prod, DivRHS, true); | 
|  | 5206 | } else {                       // (X / pos) op neg | 
|  | 5207 | Constant *DivRHSH = ConstantExpr::getNeg(SubOne(DivRHS)); | 
|  | 5208 | LoOverflow = AddWithOverflow(LoBound, Prod, | 
|  | 5209 | cast<ConstantInt>(DivRHSH), true); | 
|  | 5210 | HiBound = AddOne(Prod); | 
|  | 5211 | HiOverflow = ProdOV; | 
|  | 5212 | } | 
|  | 5213 | } else {                         // Divisor is < 0. | 
|  | 5214 | if (RHSV == 0) {       // (X / neg) op 0 | 
|  | 5215 | LoBound = AddOne(DivRHS); | 
|  | 5216 | HiBound = cast<ConstantInt>(ConstantExpr::getNeg(DivRHS)); | 
|  | 5217 | if (HiBound == DivRHS) | 
|  | 5218 | LoBound = 0;               // - INTMIN = INTMIN | 
|  | 5219 | } else if (RHSV.isPositive()) {   // (X / neg) op pos | 
|  | 5220 | HiOverflow = LoOverflow = ProdOV; | 
|  | 5221 | if (!LoOverflow) | 
|  | 5222 | LoOverflow = AddWithOverflow(LoBound, Prod, AddOne(DivRHS), | 
|  | 5223 | true); | 
|  | 5224 | HiBound = AddOne(Prod); | 
|  | 5225 | } else {                       // (X / neg) op neg | 
|  | 5226 | LoBound = Prod; | 
|  | 5227 | LoOverflow = HiOverflow = ProdOV; | 
|  | 5228 | HiBound = Subtract(Prod, DivRHS); | 
|  | 5229 | } | 
|  | 5230 |  | 
|  | 5231 | // Dividing by a negate swaps the condition. | 
|  | 5232 | predicate = ICmpInst::getSwappedPredicate(predicate); | 
|  | 5233 | } | 
|  | 5234 |  | 
|  | 5235 | if (LoBound) { | 
|  | 5236 | Value *X = LHSI->getOperand(0); | 
|  | 5237 | switch (predicate) { | 
|  | 5238 | default: assert(0 && "Unhandled icmp opcode!"); | 
|  | 5239 | case ICmpInst::ICMP_EQ: | 
|  | 5240 | if (LoOverflow && HiOverflow) | 
|  | 5241 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); | 
|  | 5242 | else if (HiOverflow) | 
|  | 5243 | return new ICmpInst(DivIsSigned ?  ICmpInst::ICMP_SGE : | 
|  | 5244 | ICmpInst::ICMP_UGE, X, LoBound); | 
|  | 5245 | else if (LoOverflow) | 
|  | 5246 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT : | 
|  | 5247 | ICmpInst::ICMP_ULT, X, HiBound); | 
|  | 5248 | else | 
|  | 5249 | return InsertRangeTest(X, LoBound, HiBound, DivIsSigned, | 
|  | 5250 | true, ICI); | 
|  | 5251 | case ICmpInst::ICMP_NE: | 
|  | 5252 | if (LoOverflow && HiOverflow) | 
|  | 5253 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue()); | 
|  | 5254 | else if (HiOverflow) | 
|  | 5255 | return new ICmpInst(DivIsSigned ?  ICmpInst::ICMP_SLT : | 
|  | 5256 | ICmpInst::ICMP_ULT, X, LoBound); | 
|  | 5257 | else if (LoOverflow) | 
|  | 5258 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE : | 
|  | 5259 | ICmpInst::ICMP_UGE, X, HiBound); | 
|  | 5260 | else | 
|  | 5261 | return InsertRangeTest(X, LoBound, HiBound, DivIsSigned, | 
|  | 5262 | false, ICI); | 
|  | 5263 | case ICmpInst::ICMP_ULT: | 
|  | 5264 | case ICmpInst::ICMP_SLT: | 
|  | 5265 | if (LoOverflow) | 
|  | 5266 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); | 
|  | 5267 | return new ICmpInst(predicate, X, LoBound); | 
|  | 5268 | case ICmpInst::ICMP_UGT: | 
|  | 5269 | case ICmpInst::ICMP_SGT: | 
|  | 5270 | if (HiOverflow) | 
|  | 5271 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); | 
|  | 5272 | if (predicate == ICmpInst::ICMP_UGT) | 
|  | 5273 | return new ICmpInst(ICmpInst::ICMP_UGE, X, HiBound); | 
|  | 5274 | else | 
|  | 5275 | return new ICmpInst(ICmpInst::ICMP_SGE, X, HiBound); | 
|  | 5276 | } | 
|  | 5277 | } | 
|  | 5278 | } | 
|  | 5279 | break; | 
|  | 5280 | } | 
|  | 5281 |  | 
|  | 5282 | // Simplify icmp_eq and icmp_ne instructions with integer constant RHS. | 
|  | 5283 | if (ICI.isEquality()) { | 
|  | 5284 | bool isICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE; | 
|  | 5285 |  | 
|  | 5286 | // If the first operand is (add|sub|and|or|xor|rem) with a constant, and | 
|  | 5287 | // the second operand is a constant, simplify a bit. | 
|  | 5288 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(LHSI)) { | 
|  | 5289 | switch (BO->getOpcode()) { | 
|  | 5290 | case Instruction::SRem: | 
|  | 5291 | // If we have a signed (X % (2^c)) == 0, turn it into an unsigned one. | 
|  | 5292 | if (RHSV == 0 && isa<ConstantInt>(BO->getOperand(1)) &&BO->hasOneUse()){ | 
|  | 5293 | const APInt &V = cast<ConstantInt>(BO->getOperand(1))->getValue(); | 
|  | 5294 | if (V.sgt(APInt(V.getBitWidth(), 1)) && V.isPowerOf2()) { | 
|  | 5295 | Instruction *NewRem = | 
|  | 5296 | BinaryOperator::createURem(BO->getOperand(0), BO->getOperand(1), | 
|  | 5297 | BO->getName()); | 
|  | 5298 | InsertNewInstBefore(NewRem, ICI); | 
|  | 5299 | return new ICmpInst(ICI.getPredicate(), NewRem, | 
|  | 5300 | Constant::getNullValue(BO->getType())); | 
|  | 5301 | } | 
|  | 5302 | } | 
|  | 5303 | break; | 
|  | 5304 | case Instruction::Add: | 
|  | 5305 | // Replace ((add A, B) != C) with (A != C-B) if B & C are constants. | 
|  | 5306 | if (ConstantInt *BOp1C = dyn_cast<ConstantInt>(BO->getOperand(1))) { | 
|  | 5307 | if (BO->hasOneUse()) | 
|  | 5308 | return new ICmpInst(ICI.getPredicate(), BO->getOperand(0), | 
|  | 5309 | Subtract(RHS, BOp1C)); | 
|  | 5310 | } else if (RHSV == 0) { | 
|  | 5311 | // Replace ((add A, B) != 0) with (A != -B) if A or B is | 
|  | 5312 | // efficiently invertible, or if the add has just this one use. | 
|  | 5313 | Value *BOp0 = BO->getOperand(0), *BOp1 = BO->getOperand(1); | 
|  | 5314 |  | 
|  | 5315 | if (Value *NegVal = dyn_castNegVal(BOp1)) | 
|  | 5316 | return new ICmpInst(ICI.getPredicate(), BOp0, NegVal); | 
|  | 5317 | else if (Value *NegVal = dyn_castNegVal(BOp0)) | 
|  | 5318 | return new ICmpInst(ICI.getPredicate(), NegVal, BOp1); | 
|  | 5319 | else if (BO->hasOneUse()) { | 
|  | 5320 | Instruction *Neg = BinaryOperator::createNeg(BOp1); | 
|  | 5321 | InsertNewInstBefore(Neg, ICI); | 
|  | 5322 | Neg->takeName(BO); | 
|  | 5323 | return new ICmpInst(ICI.getPredicate(), BOp0, Neg); | 
|  | 5324 | } | 
|  | 5325 | } | 
|  | 5326 | break; | 
|  | 5327 | case Instruction::Xor: | 
|  | 5328 | // For the xor case, we can xor two constants together, eliminating | 
|  | 5329 | // the explicit xor. | 
|  | 5330 | if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) | 
|  | 5331 | return new ICmpInst(ICI.getPredicate(), BO->getOperand(0), | 
|  | 5332 | ConstantExpr::getXor(RHS, BOC)); | 
|  | 5333 |  | 
|  | 5334 | // FALLTHROUGH | 
|  | 5335 | case Instruction::Sub: | 
|  | 5336 | // Replace (([sub|xor] A, B) != 0) with (A != B) | 
|  | 5337 | if (RHSV == 0) | 
|  | 5338 | return new ICmpInst(ICI.getPredicate(), BO->getOperand(0), | 
|  | 5339 | BO->getOperand(1)); | 
|  | 5340 | break; | 
|  | 5341 |  | 
|  | 5342 | case Instruction::Or: | 
|  | 5343 | // If bits are being or'd in that are not present in the constant we | 
|  | 5344 | // are comparing against, then the comparison could never succeed! | 
|  | 5345 | if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) { | 
|  | 5346 | Constant *NotCI = ConstantExpr::getNot(RHS); | 
|  | 5347 | if (!ConstantExpr::getAnd(BOC, NotCI)->isNullValue()) | 
|  | 5348 | return ReplaceInstUsesWith(ICI, ConstantInt::get(Type::Int1Ty, | 
|  | 5349 | isICMP_NE)); | 
|  | 5350 | } | 
|  | 5351 | break; | 
|  | 5352 |  | 
|  | 5353 | case Instruction::And: | 
|  | 5354 | if (ConstantInt *BOC = dyn_cast<ConstantInt>(BO->getOperand(1))) { | 
|  | 5355 | // If bits are being compared against that are and'd out, then the | 
|  | 5356 | // comparison can never succeed! | 
|  | 5357 | if ((RHSV & ~BOC->getValue()) != 0) | 
|  | 5358 | return ReplaceInstUsesWith(ICI, ConstantInt::get(Type::Int1Ty, | 
|  | 5359 | isICMP_NE)); | 
|  | 5360 |  | 
|  | 5361 | // If we have ((X & C) == C), turn it into ((X & C) != 0). | 
|  | 5362 | if (RHS == BOC && RHSV.isPowerOf2()) | 
|  | 5363 | return new ICmpInst(isICMP_NE ? ICmpInst::ICMP_EQ : | 
|  | 5364 | ICmpInst::ICMP_NE, LHSI, | 
|  | 5365 | Constant::getNullValue(RHS->getType())); | 
|  | 5366 |  | 
|  | 5367 | // Replace (and X, (1 << size(X)-1) != 0) with x s< 0 | 
|  | 5368 | if (isSignBit(BOC)) { | 
|  | 5369 | Value *X = BO->getOperand(0); | 
|  | 5370 | Constant *Zero = Constant::getNullValue(X->getType()); | 
|  | 5371 | ICmpInst::Predicate pred = isICMP_NE ? | 
|  | 5372 | ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGE; | 
|  | 5373 | return new ICmpInst(pred, X, Zero); | 
|  | 5374 | } | 
|  | 5375 |  | 
|  | 5376 | // ((X & ~7) == 0) --> X < 8 | 
|  | 5377 | if (RHSV == 0 && isHighOnes(BOC)) { | 
|  | 5378 | Value *X = BO->getOperand(0); | 
|  | 5379 | Constant *NegX = ConstantExpr::getNeg(BOC); | 
|  | 5380 | ICmpInst::Predicate pred = isICMP_NE ? | 
|  | 5381 | ICmpInst::ICMP_UGE : ICmpInst::ICMP_ULT; | 
|  | 5382 | return new ICmpInst(pred, X, NegX); | 
|  | 5383 | } | 
|  | 5384 | } | 
|  | 5385 | default: break; | 
|  | 5386 | } | 
|  | 5387 | } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(LHSI)) { | 
|  | 5388 | // Handle icmp {eq|ne} <intrinsic>, intcst. | 
|  | 5389 | if (II->getIntrinsicID() == Intrinsic::bswap) { | 
|  | 5390 | AddToWorkList(II); | 
|  | 5391 | ICI.setOperand(0, II->getOperand(1)); | 
|  | 5392 | ICI.setOperand(1, ConstantInt::get(RHSV.byteSwap())); | 
|  | 5393 | return &ICI; | 
|  | 5394 | } | 
|  | 5395 | } | 
|  | 5396 | } else {  // Not a ICMP_EQ/ICMP_NE | 
|  | 5397 | // If the LHS is a cast from an integral value of the same size, then | 
|  | 5398 | // since we know the RHS is a constant, try to simlify. | 
|  | 5399 | if (CastInst *Cast = dyn_cast<CastInst>(LHSI)) { | 
|  | 5400 | Value *CastOp = Cast->getOperand(0); | 
|  | 5401 | const Type *SrcTy = CastOp->getType(); | 
|  | 5402 | uint32_t SrcTySize = SrcTy->getPrimitiveSizeInBits(); | 
|  | 5403 | if (SrcTy->isInteger() && | 
|  | 5404 | SrcTySize == Cast->getType()->getPrimitiveSizeInBits()) { | 
|  | 5405 | // If this is an unsigned comparison, try to make the comparison use | 
|  | 5406 | // smaller constant values. | 
|  | 5407 | if (ICI.getPredicate() == ICmpInst::ICMP_ULT && RHSV.isSignBit()) { | 
|  | 5408 | // X u< 128 => X s> -1 | 
|  | 5409 | return new ICmpInst(ICmpInst::ICMP_SGT, CastOp, | 
|  | 5410 | ConstantInt::get(APInt::getAllOnesValue(SrcTySize))); | 
|  | 5411 | } else if (ICI.getPredicate() == ICmpInst::ICMP_UGT && | 
|  | 5412 | RHSV == APInt::getSignedMaxValue(SrcTySize)) { | 
|  | 5413 | // X u> 127 => X s< 0 | 
|  | 5414 | return new ICmpInst(ICmpInst::ICMP_SLT, CastOp, | 
|  | 5415 | Constant::getNullValue(SrcTy)); | 
|  | 5416 | } | 
|  | 5417 | } | 
|  | 5418 | } | 
|  | 5419 | } | 
|  | 5420 | return 0; | 
|  | 5421 | } | 
|  | 5422 |  | 
|  | 5423 | /// visitICmpInstWithCastAndCast - Handle icmp (cast x to y), (cast/cst). | 
|  | 5424 | /// We only handle extending casts so far. | 
|  | 5425 | /// | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5426 | Instruction *InstCombiner::visitICmpInstWithCastAndCast(ICmpInst &ICI) { | 
|  | 5427 | const CastInst *LHSCI = cast<CastInst>(ICI.getOperand(0)); | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 5428 | Value *LHSCIOp        = LHSCI->getOperand(0); | 
|  | 5429 | const Type *SrcTy     = LHSCIOp->getType(); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5430 | const Type *DestTy    = LHSCI->getType(); | 
| Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5431 | Value *RHSCIOp; | 
|  | 5432 |  | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5433 | // We only handle extension cast instructions, so far. Enforce this. | 
|  | 5434 | if (LHSCI->getOpcode() != Instruction::ZExt && | 
|  | 5435 | LHSCI->getOpcode() != Instruction::SExt) | 
| Chris Lattner | 03f06f1 | 2005-01-17 03:20:02 +0000 | [diff] [blame] | 5436 | return 0; | 
|  | 5437 |  | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5438 | bool isSignedExt = LHSCI->getOpcode() == Instruction::SExt; | 
|  | 5439 | bool isSignedCmp = ICI.isSignedPredicate(); | 
| Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5440 |  | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5441 | if (CastInst *CI = dyn_cast<CastInst>(ICI.getOperand(1))) { | 
| Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5442 | // Not an extension from the same type? | 
|  | 5443 | RHSCIOp = CI->getOperand(0); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5444 | if (RHSCIOp->getType() != LHSCIOp->getType()) | 
|  | 5445 | return 0; | 
| Chris Lattner | 387bf3f | 2007-01-13 23:11:38 +0000 | [diff] [blame] | 5446 |  | 
|  | 5447 | // If the signedness of the two compares doesn't agree (i.e. one is a sext | 
|  | 5448 | // and the other is a zext), then we can't handle this. | 
|  | 5449 | if (CI->getOpcode() != LHSCI->getOpcode()) | 
|  | 5450 | return 0; | 
|  | 5451 |  | 
|  | 5452 | // Likewise, if the signedness of the [sz]exts and the compare don't match, | 
|  | 5453 | // then we can't handle this. | 
|  | 5454 | if (isSignedExt != isSignedCmp && !ICI.isEquality()) | 
|  | 5455 | return 0; | 
|  | 5456 |  | 
|  | 5457 | // Okay, just insert a compare of the reduced operands now! | 
|  | 5458 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSCIOp); | 
| Reid Spencer | 279fa25 | 2004-11-28 21:31:15 +0000 | [diff] [blame] | 5459 | } | 
| Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5460 |  | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5461 | // If we aren't dealing with a constant on the RHS, exit early | 
|  | 5462 | ConstantInt *CI = dyn_cast<ConstantInt>(ICI.getOperand(1)); | 
|  | 5463 | if (!CI) | 
|  | 5464 | return 0; | 
|  | 5465 |  | 
|  | 5466 | // Compute the constant that would happen if we truncated to SrcTy then | 
|  | 5467 | // reextended to DestTy. | 
|  | 5468 | Constant *Res1 = ConstantExpr::getTrunc(CI, SrcTy); | 
|  | 5469 | Constant *Res2 = ConstantExpr::getCast(LHSCI->getOpcode(), Res1, DestTy); | 
|  | 5470 |  | 
|  | 5471 | // If the re-extended constant didn't change... | 
|  | 5472 | if (Res2 == CI) { | 
|  | 5473 | // Make sure that sign of the Cmp and the sign of the Cast are the same. | 
|  | 5474 | // For example, we might have: | 
|  | 5475 | //    %A = sext short %X to uint | 
|  | 5476 | //    %B = icmp ugt uint %A, 1330 | 
|  | 5477 | // It is incorrect to transform this into | 
|  | 5478 | //    %B = icmp ugt short %X, 1330 | 
|  | 5479 | // because %A may have negative value. | 
|  | 5480 | // | 
|  | 5481 | // However, it is OK if SrcTy is bool (See cast-set.ll testcase) | 
|  | 5482 | // OR operation is EQ/NE. | 
| Reid Spencer | 542964f | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 5483 | if (isSignedExt == isSignedCmp || SrcTy == Type::Int1Ty || ICI.isEquality()) | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5484 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, Res1); | 
|  | 5485 | else | 
|  | 5486 | return 0; | 
|  | 5487 | } | 
|  | 5488 |  | 
|  | 5489 | // The re-extended constant changed so the constant cannot be represented | 
|  | 5490 | // in the shorter type. Consequently, we cannot emit a simple comparison. | 
|  | 5491 |  | 
|  | 5492 | // First, handle some easy cases. We know the result cannot be equal at this | 
|  | 5493 | // point so handle the ICI.isEquality() cases | 
|  | 5494 | if (ICI.getPredicate() == ICmpInst::ICMP_EQ) | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5495 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse()); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5496 | if (ICI.getPredicate() == ICmpInst::ICMP_NE) | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5497 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue()); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5498 |  | 
|  | 5499 | // Evaluate the comparison for LT (we invert for GT below). LE and GE cases | 
|  | 5500 | // should have been folded away previously and not enter in here. | 
|  | 5501 | Value *Result; | 
|  | 5502 | if (isSignedCmp) { | 
|  | 5503 | // We're performing a signed comparison. | 
| Reid Spencer | c3e3b8a | 2007-03-22 20:36:03 +0000 | [diff] [blame] | 5504 | if (cast<ConstantInt>(CI)->getValue().isNegative()) | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5505 | Result = ConstantInt::getFalse();          // X < (small) --> false | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5506 | else | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5507 | Result = ConstantInt::getTrue();           // X < (large) --> true | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5508 | } else { | 
|  | 5509 | // We're performing an unsigned comparison. | 
|  | 5510 | if (isSignedExt) { | 
|  | 5511 | // We're performing an unsigned comp with a sign extended value. | 
|  | 5512 | // This is true if the input is >= 0. [aka >s -1] | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5513 | Constant *NegOne = ConstantInt::getAllOnesValue(SrcTy); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5514 | Result = InsertNewInstBefore(new ICmpInst(ICmpInst::ICMP_SGT, LHSCIOp, | 
|  | 5515 | NegOne, ICI.getName()), ICI); | 
|  | 5516 | } else { | 
|  | 5517 | // Unsigned extend & unsigned compare -> always true. | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5518 | Result = ConstantInt::getTrue(); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5519 | } | 
|  | 5520 | } | 
|  | 5521 |  | 
|  | 5522 | // Finally, return the value computed. | 
|  | 5523 | if (ICI.getPredicate() == ICmpInst::ICMP_ULT || | 
|  | 5524 | ICI.getPredicate() == ICmpInst::ICMP_SLT) { | 
|  | 5525 | return ReplaceInstUsesWith(ICI, Result); | 
|  | 5526 | } else { | 
|  | 5527 | assert((ICI.getPredicate()==ICmpInst::ICMP_UGT || | 
|  | 5528 | ICI.getPredicate()==ICmpInst::ICMP_SGT) && | 
|  | 5529 | "ICmp should be folded!"); | 
|  | 5530 | if (Constant *CI = dyn_cast<Constant>(Result)) | 
|  | 5531 | return ReplaceInstUsesWith(ICI, ConstantExpr::getNot(CI)); | 
|  | 5532 | else | 
|  | 5533 | return BinaryOperator::createNot(Result); | 
|  | 5534 | } | 
| Chris Lattner | d1f46d3 | 2005-04-24 06:59:08 +0000 | [diff] [blame] | 5535 | } | 
| Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5536 |  | 
| Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 5537 | Instruction *InstCombiner::visitShl(BinaryOperator &I) { | 
|  | 5538 | return commonShiftTransforms(I); | 
|  | 5539 | } | 
|  | 5540 |  | 
|  | 5541 | Instruction *InstCombiner::visitLShr(BinaryOperator &I) { | 
|  | 5542 | return commonShiftTransforms(I); | 
|  | 5543 | } | 
|  | 5544 |  | 
|  | 5545 | Instruction *InstCombiner::visitAShr(BinaryOperator &I) { | 
|  | 5546 | return commonShiftTransforms(I); | 
|  | 5547 | } | 
|  | 5548 |  | 
|  | 5549 | Instruction *InstCombiner::commonShiftTransforms(BinaryOperator &I) { | 
|  | 5550 | assert(I.getOperand(1)->getType() == I.getOperand(0)->getType()); | 
| Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 5551 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); | 
| Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5552 |  | 
|  | 5553 | // shl X, 0 == X and shr X, 0 == X | 
|  | 5554 | // shl 0, X == 0 and shr 0, X == 0 | 
| Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 5555 | if (Op1 == Constant::getNullValue(Op1->getType()) || | 
| Chris Lattner | e679449 | 2002-08-12 21:17:25 +0000 | [diff] [blame] | 5556 | Op0 == Constant::getNullValue(Op0->getType())) | 
|  | 5557 | return ReplaceInstUsesWith(I, Op0); | 
| Chris Lattner | f5b4ef7 | 2006-02-12 08:07:37 +0000 | [diff] [blame] | 5558 |  | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5559 | if (isa<UndefValue>(Op0)) { | 
|  | 5560 | if (I.getOpcode() == Instruction::AShr) // undef >>s X -> undef | 
| Chris Lattner | 67f0545 | 2004-10-16 23:28:04 +0000 | [diff] [blame] | 5561 | return ReplaceInstUsesWith(I, Op0); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5562 | else                                    // undef << X -> 0, undef >>u X -> 0 | 
| Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 5563 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); | 
|  | 5564 | } | 
|  | 5565 | if (isa<UndefValue>(Op1)) { | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5566 | if (I.getOpcode() == Instruction::AShr)  // X >>s undef -> X | 
|  | 5567 | return ReplaceInstUsesWith(I, Op0); | 
|  | 5568 | else                                     // X << undef, X >>u undef -> 0 | 
| Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 5569 | return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType())); | 
| Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 5570 | } | 
|  | 5571 |  | 
| Chris Lattner | d4dee40 | 2006-11-10 23:38:52 +0000 | [diff] [blame] | 5572 | // ashr int -1, X = -1   (for any arithmetic shift rights of ~0) | 
|  | 5573 | if (I.getOpcode() == Instruction::AShr) | 
| Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5574 | if (ConstantInt *CSI = dyn_cast<ConstantInt>(Op0)) | 
| Chris Lattner | d4dee40 | 2006-11-10 23:38:52 +0000 | [diff] [blame] | 5575 | if (CSI->isAllOnesValue()) | 
| Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 5576 | return ReplaceInstUsesWith(I, CSI); | 
|  | 5577 |  | 
| Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 5578 | // Try to fold constant and into select arguments. | 
|  | 5579 | if (isa<Constant>(Op0)) | 
|  | 5580 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) | 
| Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 5581 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) | 
| Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 5582 | return R; | 
|  | 5583 |  | 
| Chris Lattner | b18dbbf | 2005-05-08 17:34:56 +0000 | [diff] [blame] | 5584 | // See if we can turn a signed shr into an unsigned shr. | 
| Chris Lattner | b3f24c9 | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 5585 | if (I.isArithmeticShift()) { | 
| Reid Spencer | 6274c72 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 5586 | if (MaskedValueIsZero(Op0, | 
|  | 5587 | APInt::getSignBit(I.getType()->getPrimitiveSizeInBits()))) { | 
| Reid Spencer | 0d5f923 | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 5588 | return BinaryOperator::createLShr(Op0, Op1, I.getName()); | 
| Chris Lattner | b18dbbf | 2005-05-08 17:34:56 +0000 | [diff] [blame] | 5589 | } | 
|  | 5590 | } | 
| Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 5591 |  | 
| Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5592 | if (ConstantInt *CUI = dyn_cast<ConstantInt>(Op1)) | 
| Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 5593 | if (Instruction *Res = FoldShiftByConstant(Op0, CUI, I)) | 
|  | 5594 | return Res; | 
| Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5595 | return 0; | 
|  | 5596 | } | 
|  | 5597 |  | 
| Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5598 | Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantInt *Op1, | 
| Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 5599 | BinaryOperator &I) { | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 5600 | bool isLeftShift    = I.getOpcode() == Instruction::Shl; | 
| Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5601 |  | 
| Chris Lattner | f5b4ef7 | 2006-02-12 08:07:37 +0000 | [diff] [blame] | 5602 | // See if we can simplify any instructions used by the instruction whose sole | 
|  | 5603 | // purpose is to compute bits we don't care about. | 
| Reid Spencer | 6274c72 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 5604 | uint32_t TypeBits = Op0->getType()->getPrimitiveSizeInBits(); | 
|  | 5605 | APInt KnownZero(TypeBits, 0), KnownOne(TypeBits, 0); | 
|  | 5606 | if (SimplifyDemandedBits(&I, APInt::getAllOnesValue(TypeBits), | 
| Chris Lattner | f5b4ef7 | 2006-02-12 08:07:37 +0000 | [diff] [blame] | 5607 | KnownZero, KnownOne)) | 
|  | 5608 | return &I; | 
|  | 5609 |  | 
| Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5610 | // shl uint X, 32 = 0 and shr ubyte Y, 9 = 0, ... just don't eliminate shr | 
|  | 5611 | // of a signed value. | 
|  | 5612 | // | 
| Zhou Sheng | b25806f | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 5613 | if (Op1->uge(TypeBits)) { | 
| Chris Lattner | d5fea61 | 2007-02-02 05:29:55 +0000 | [diff] [blame] | 5614 | if (I.getOpcode() != Instruction::AShr) | 
| Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5615 | return ReplaceInstUsesWith(I, Constant::getNullValue(Op0->getType())); | 
|  | 5616 | else { | 
| Chris Lattner | d5fea61 | 2007-02-02 05:29:55 +0000 | [diff] [blame] | 5617 | I.setOperand(1, ConstantInt::get(I.getType(), TypeBits-1)); | 
| Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5618 | return &I; | 
| Chris Lattner | f5ce254 | 2004-02-23 20:30:06 +0000 | [diff] [blame] | 5619 | } | 
| Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5620 | } | 
|  | 5621 |  | 
|  | 5622 | // ((X*C1) << C2) == (X * (C1 << C2)) | 
|  | 5623 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0)) | 
|  | 5624 | if (BO->getOpcode() == Instruction::Mul && isLeftShift) | 
|  | 5625 | if (Constant *BOOp = dyn_cast<Constant>(BO->getOperand(1))) | 
|  | 5626 | return BinaryOperator::createMul(BO->getOperand(0), | 
|  | 5627 | ConstantExpr::getShl(BOOp, Op1)); | 
|  | 5628 |  | 
|  | 5629 | // Try to fold constant and into select arguments. | 
|  | 5630 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) | 
|  | 5631 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) | 
|  | 5632 | return R; | 
|  | 5633 | if (isa<PHINode>(Op0)) | 
|  | 5634 | if (Instruction *NV = FoldOpIntoPhi(I)) | 
|  | 5635 | return NV; | 
|  | 5636 |  | 
|  | 5637 | if (Op0->hasOneUse()) { | 
| Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5638 | if (BinaryOperator *Op0BO = dyn_cast<BinaryOperator>(Op0)) { | 
|  | 5639 | // Turn ((X >> C) + Y) << C  ->  (X + (Y << C)) & (~0 << C) | 
|  | 5640 | Value *V1, *V2; | 
|  | 5641 | ConstantInt *CC; | 
|  | 5642 | switch (Op0BO->getOpcode()) { | 
| Chris Lattner | 27cb9db | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 5643 | default: break; | 
|  | 5644 | case Instruction::Add: | 
|  | 5645 | case Instruction::And: | 
|  | 5646 | case Instruction::Or: | 
| Reid Spencer | 2f34b98 | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 5647 | case Instruction::Xor: { | 
| Chris Lattner | 27cb9db | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 5648 | // These operators commute. | 
|  | 5649 | // Turn (Y + (X >> C)) << C  ->  (X + (Y << C)) & (~0 << C) | 
| Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5650 | if (isLeftShift && Op0BO->getOperand(1)->hasOneUse() && | 
|  | 5651 | match(Op0BO->getOperand(1), | 
| Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5652 | m_Shr(m_Value(V1), m_ConstantInt(CC))) && CC == Op1) { | 
| Reid Spencer | 0d5f923 | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 5653 | Instruction *YS = BinaryOperator::createShl( | 
| Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5654 | Op0BO->getOperand(0), Op1, | 
| Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5655 | Op0BO->getName()); | 
|  | 5656 | InsertNewInstBefore(YS, I); // (Y << C) | 
| Chris Lattner | 24cd2fa | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 5657 | Instruction *X = | 
|  | 5658 | BinaryOperator::create(Op0BO->getOpcode(), YS, V1, | 
|  | 5659 | Op0BO->getOperand(1)->getName()); | 
| Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5660 | InsertNewInstBefore(X, I);  // (X + (Y << C)) | 
| Zhou Sheng | fd28a33 | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 5661 | uint32_t Op1Val = Op1->getLimitedValue(TypeBits); | 
| Zhou Sheng | 5e60a4a | 2007-03-30 05:45:18 +0000 | [diff] [blame] | 5662 | return BinaryOperator::createAnd(X, ConstantInt::get( | 
|  | 5663 | APInt::getHighBitsSet(TypeBits, TypeBits-Op1Val))); | 
| Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5664 | } | 
| Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5665 |  | 
| Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5666 | // Turn (Y + ((X >> C) & CC)) << C  ->  ((X & (CC << C)) + (Y << C)) | 
| Reid Spencer | 2f34b98 | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 5667 | Value *Op0BOOp1 = Op0BO->getOperand(1); | 
| Chris Lattner | fe53cf2 | 2007-03-05 00:11:19 +0000 | [diff] [blame] | 5668 | if (isLeftShift && Op0BOOp1->hasOneUse() && | 
| Reid Spencer | 2f34b98 | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 5669 | match(Op0BOOp1, | 
|  | 5670 | m_And(m_Shr(m_Value(V1), m_Value(V2)),m_ConstantInt(CC))) && | 
| Chris Lattner | fe53cf2 | 2007-03-05 00:11:19 +0000 | [diff] [blame] | 5671 | cast<BinaryOperator>(Op0BOOp1)->getOperand(0)->hasOneUse() && | 
|  | 5672 | V2 == Op1) { | 
| Reid Spencer | 0d5f923 | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 5673 | Instruction *YS = BinaryOperator::createShl( | 
| Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 5674 | Op0BO->getOperand(0), Op1, | 
|  | 5675 | Op0BO->getName()); | 
| Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5676 | InsertNewInstBefore(YS, I); // (Y << C) | 
|  | 5677 | Instruction *XM = | 
| Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5678 | BinaryOperator::createAnd(V1, ConstantExpr::getShl(CC, Op1), | 
| Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5679 | V1->getName()+".mask"); | 
|  | 5680 | InsertNewInstBefore(XM, I); // X & (CC << C) | 
|  | 5681 |  | 
|  | 5682 | return BinaryOperator::create(Op0BO->getOpcode(), YS, XM); | 
|  | 5683 | } | 
| Reid Spencer | 2f34b98 | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 5684 | } | 
| Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5685 |  | 
| Reid Spencer | 2f34b98 | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 5686 | // FALL THROUGH. | 
|  | 5687 | case Instruction::Sub: { | 
| Chris Lattner | 27cb9db | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 5688 | // Turn ((X >> C) + Y) << C  ->  (X + (Y << C)) & (~0 << C) | 
| Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5689 | if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() && | 
|  | 5690 | match(Op0BO->getOperand(0), | 
| Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5691 | m_Shr(m_Value(V1), m_ConstantInt(CC))) && CC == Op1) { | 
| Reid Spencer | 0d5f923 | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 5692 | Instruction *YS = BinaryOperator::createShl( | 
| Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 5693 | Op0BO->getOperand(1), Op1, | 
|  | 5694 | Op0BO->getName()); | 
| Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5695 | InsertNewInstBefore(YS, I); // (Y << C) | 
| Chris Lattner | 24cd2fa | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 5696 | Instruction *X = | 
| Chris Lattner | 1df0e98 | 2006-05-31 21:14:00 +0000 | [diff] [blame] | 5697 | BinaryOperator::create(Op0BO->getOpcode(), V1, YS, | 
| Chris Lattner | 24cd2fa | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 5698 | Op0BO->getOperand(0)->getName()); | 
| Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5699 | InsertNewInstBefore(X, I);  // (X + (Y << C)) | 
| Zhou Sheng | fd28a33 | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 5700 | uint32_t Op1Val = Op1->getLimitedValue(TypeBits); | 
| Zhou Sheng | 5e60a4a | 2007-03-30 05:45:18 +0000 | [diff] [blame] | 5701 | return BinaryOperator::createAnd(X, ConstantInt::get( | 
|  | 5702 | APInt::getHighBitsSet(TypeBits, TypeBits-Op1Val))); | 
| Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5703 | } | 
| Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5704 |  | 
| Chris Lattner | 1df0e98 | 2006-05-31 21:14:00 +0000 | [diff] [blame] | 5705 | // Turn (((X >> C)&CC) + Y) << C  ->  (X + (Y << C)) & (CC << C) | 
| Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5706 | if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() && | 
|  | 5707 | match(Op0BO->getOperand(0), | 
|  | 5708 | m_And(m_Shr(m_Value(V1), m_Value(V2)), | 
| Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5709 | m_ConstantInt(CC))) && V2 == Op1 && | 
| Chris Lattner | 24cd2fa | 2006-02-09 07:41:14 +0000 | [diff] [blame] | 5710 | cast<BinaryOperator>(Op0BO->getOperand(0)) | 
|  | 5711 | ->getOperand(0)->hasOneUse()) { | 
| Reid Spencer | 0d5f923 | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 5712 | Instruction *YS = BinaryOperator::createShl( | 
| Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 5713 | Op0BO->getOperand(1), Op1, | 
|  | 5714 | Op0BO->getName()); | 
| Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5715 | InsertNewInstBefore(YS, I); // (Y << C) | 
|  | 5716 | Instruction *XM = | 
| Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5717 | BinaryOperator::createAnd(V1, ConstantExpr::getShl(CC, Op1), | 
| Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5718 | V1->getName()+".mask"); | 
|  | 5719 | InsertNewInstBefore(XM, I); // X & (CC << C) | 
|  | 5720 |  | 
| Chris Lattner | 1df0e98 | 2006-05-31 21:14:00 +0000 | [diff] [blame] | 5721 | return BinaryOperator::create(Op0BO->getOpcode(), XM, YS); | 
| Chris Lattner | 797dee7 | 2005-09-18 06:30:59 +0000 | [diff] [blame] | 5722 | } | 
| Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5723 |  | 
| Chris Lattner | 27cb9db | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 5724 | break; | 
| Reid Spencer | 2f34b98 | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 5725 | } | 
| Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5726 | } | 
|  | 5727 |  | 
|  | 5728 |  | 
|  | 5729 | // If the operand is an bitwise operator with a constant RHS, and the | 
|  | 5730 | // shift is the only use, we can pull it out of the shift. | 
|  | 5731 | if (ConstantInt *Op0C = dyn_cast<ConstantInt>(Op0BO->getOperand(1))) { | 
|  | 5732 | bool isValid = true;     // Valid only for And, Or, Xor | 
|  | 5733 | bool highBitSet = false; // Transform if high bit of constant set? | 
|  | 5734 |  | 
|  | 5735 | switch (Op0BO->getOpcode()) { | 
| Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 5736 | default: isValid = false; break;   // Do not perform transform! | 
| Chris Lattner | 44bd392 | 2004-10-08 03:46:20 +0000 | [diff] [blame] | 5737 | case Instruction::Add: | 
|  | 5738 | isValid = isLeftShift; | 
|  | 5739 | break; | 
| Chris Lattner | deaa0dd | 2003-08-12 21:53:41 +0000 | [diff] [blame] | 5740 | case Instruction::Or: | 
|  | 5741 | case Instruction::Xor: | 
|  | 5742 | highBitSet = false; | 
|  | 5743 | break; | 
|  | 5744 | case Instruction::And: | 
|  | 5745 | highBitSet = true; | 
|  | 5746 | break; | 
| Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5747 | } | 
|  | 5748 |  | 
|  | 5749 | // If this is a signed shift right, and the high bit is modified | 
|  | 5750 | // by the logical operation, do not perform the transformation. | 
|  | 5751 | // The highBitSet boolean indicates the value of the high bit of | 
|  | 5752 | // the constant which would cause it to be modified for this | 
|  | 5753 | // operation. | 
|  | 5754 | // | 
| Chris Lattner | 3e009e8 | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5755 | if (isValid && !isLeftShift && I.getOpcode() == Instruction::AShr) { | 
| Zhou Sheng | 23f7a1c | 2007-03-28 15:02:20 +0000 | [diff] [blame] | 5756 | isValid = Op0C->getValue()[TypeBits-1] == highBitSet; | 
| Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5757 | } | 
|  | 5758 |  | 
|  | 5759 | if (isValid) { | 
|  | 5760 | Constant *NewRHS = ConstantExpr::get(I.getOpcode(), Op0C, Op1); | 
|  | 5761 |  | 
|  | 5762 | Instruction *NewShift = | 
| Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 5763 | BinaryOperator::create(I.getOpcode(), Op0BO->getOperand(0), Op1); | 
| Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5764 | InsertNewInstBefore(NewShift, I); | 
| Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 5765 | NewShift->takeName(Op0BO); | 
| Chris Lattner | 1455393 | 2006-01-06 07:12:35 +0000 | [diff] [blame] | 5766 |  | 
|  | 5767 | return BinaryOperator::create(Op0BO->getOpcode(), NewShift, | 
|  | 5768 | NewRHS); | 
|  | 5769 | } | 
|  | 5770 | } | 
|  | 5771 | } | 
|  | 5772 | } | 
|  | 5773 |  | 
| Chris Lattner | eb372a0 | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 5774 | // Find out if this is a shift of a shift by a constant. | 
| Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 5775 | BinaryOperator *ShiftOp = dyn_cast<BinaryOperator>(Op0); | 
|  | 5776 | if (ShiftOp && !ShiftOp->isShift()) | 
|  | 5777 | ShiftOp = 0; | 
| Chris Lattner | eb372a0 | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 5778 |  | 
| Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5779 | if (ShiftOp && isa<ConstantInt>(ShiftOp->getOperand(1))) { | 
| Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5780 | ConstantInt *ShiftAmt1C = cast<ConstantInt>(ShiftOp->getOperand(1)); | 
| Zhou Sheng | b25806f | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 5781 | uint32_t ShiftAmt1 = ShiftAmt1C->getLimitedValue(TypeBits); | 
|  | 5782 | uint32_t ShiftAmt2 = Op1->getLimitedValue(TypeBits); | 
| Chris Lattner | 3e009e8 | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5783 | assert(ShiftAmt2 != 0 && "Should have been simplified earlier"); | 
|  | 5784 | if (ShiftAmt1 == 0) return 0;  // Will be simplified in the future. | 
|  | 5785 | Value *X = ShiftOp->getOperand(0); | 
| Chris Lattner | eb372a0 | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 5786 |  | 
| Zhou Sheng | 56cda95 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 5787 | uint32_t AmtSum = ShiftAmt1+ShiftAmt2;   // Fold into one big shift. | 
| Reid Spencer | 6274c72 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 5788 | if (AmtSum > TypeBits) | 
|  | 5789 | AmtSum = TypeBits; | 
| Chris Lattner | 3e009e8 | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5790 |  | 
|  | 5791 | const IntegerType *Ty = cast<IntegerType>(I.getType()); | 
|  | 5792 |  | 
|  | 5793 | // Check for (X << c1) << c2  and  (X >> c1) >> c2 | 
| Chris Lattner | 6c344e5 | 2007-02-03 23:28:07 +0000 | [diff] [blame] | 5794 | if (I.getOpcode() == ShiftOp->getOpcode()) { | 
| Chris Lattner | 3e009e8 | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5795 | return BinaryOperator::create(I.getOpcode(), X, | 
|  | 5796 | ConstantInt::get(Ty, AmtSum)); | 
|  | 5797 | } else if (ShiftOp->getOpcode() == Instruction::LShr && | 
|  | 5798 | I.getOpcode() == Instruction::AShr) { | 
|  | 5799 | // ((X >>u C1) >>s C2) -> (X >>u (C1+C2))  since C1 != 0. | 
|  | 5800 | return BinaryOperator::createLShr(X, ConstantInt::get(Ty, AmtSum)); | 
|  | 5801 | } else if (ShiftOp->getOpcode() == Instruction::AShr && | 
|  | 5802 | I.getOpcode() == Instruction::LShr) { | 
|  | 5803 | // ((X >>s C1) >>u C2) -> ((X >>s (C1+C2)) & mask) since C1 != 0. | 
|  | 5804 | Instruction *Shift = | 
|  | 5805 | BinaryOperator::createAShr(X, ConstantInt::get(Ty, AmtSum)); | 
|  | 5806 | InsertNewInstBefore(Shift, I); | 
|  | 5807 |  | 
| Zhou Sheng | 23f7a1c | 2007-03-28 15:02:20 +0000 | [diff] [blame] | 5808 | APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2)); | 
| Reid Spencer | 6274c72 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 5809 | return BinaryOperator::createAnd(Shift, ConstantInt::get(Mask)); | 
| Chris Lattner | eb372a0 | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 5810 | } | 
|  | 5811 |  | 
| Chris Lattner | 3e009e8 | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5812 | // Okay, if we get here, one shift must be left, and the other shift must be | 
|  | 5813 | // right.  See if the amounts are equal. | 
|  | 5814 | if (ShiftAmt1 == ShiftAmt2) { | 
|  | 5815 | // If we have ((X >>? C) << C), turn this into X & (-1 << C). | 
|  | 5816 | if (I.getOpcode() == Instruction::Shl) { | 
| Reid Spencer | 5283032 | 2007-03-25 21:11:44 +0000 | [diff] [blame] | 5817 | APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt1)); | 
| Reid Spencer | 6274c72 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 5818 | return BinaryOperator::createAnd(X, ConstantInt::get(Mask)); | 
| Chris Lattner | 3e009e8 | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5819 | } | 
|  | 5820 | // If we have ((X << C) >>u C), turn this into X & (-1 >>u C). | 
|  | 5821 | if (I.getOpcode() == Instruction::LShr) { | 
| Zhou Sheng | 150f3bb | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 5822 | APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt1)); | 
| Reid Spencer | 6274c72 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 5823 | return BinaryOperator::createAnd(X, ConstantInt::get(Mask)); | 
| Chris Lattner | 3e009e8 | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5824 | } | 
|  | 5825 | // We can simplify ((X << C) >>s C) into a trunc + sext. | 
|  | 5826 | // NOTE: we could do this for any C, but that would make 'unusual' integer | 
|  | 5827 | // types.  For now, just stick to ones well-supported by the code | 
|  | 5828 | // generators. | 
|  | 5829 | const Type *SExtType = 0; | 
|  | 5830 | switch (Ty->getBitWidth() - ShiftAmt1) { | 
| Zhou Sheng | 23f7a1c | 2007-03-28 15:02:20 +0000 | [diff] [blame] | 5831 | case 1  : | 
|  | 5832 | case 8  : | 
|  | 5833 | case 16 : | 
|  | 5834 | case 32 : | 
|  | 5835 | case 64 : | 
|  | 5836 | case 128: | 
|  | 5837 | SExtType = IntegerType::get(Ty->getBitWidth() - ShiftAmt1); | 
|  | 5838 | break; | 
| Chris Lattner | 3e009e8 | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5839 | default: break; | 
|  | 5840 | } | 
|  | 5841 | if (SExtType) { | 
|  | 5842 | Instruction *NewTrunc = new TruncInst(X, SExtType, "sext"); | 
|  | 5843 | InsertNewInstBefore(NewTrunc, I); | 
|  | 5844 | return new SExtInst(NewTrunc, Ty); | 
|  | 5845 | } | 
|  | 5846 | // Otherwise, we can't handle it yet. | 
|  | 5847 | } else if (ShiftAmt1 < ShiftAmt2) { | 
| Zhou Sheng | 56cda95 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 5848 | uint32_t ShiftDiff = ShiftAmt2-ShiftAmt1; | 
| Chris Lattner | eb372a0 | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 5849 |  | 
| Chris Lattner | 83ac5ae9 | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 5850 | // (X >>? C1) << C2 --> X << (C2-C1) & (-1 << C2) | 
| Chris Lattner | 3e009e8 | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5851 | if (I.getOpcode() == Instruction::Shl) { | 
|  | 5852 | assert(ShiftOp->getOpcode() == Instruction::LShr || | 
|  | 5853 | ShiftOp->getOpcode() == Instruction::AShr); | 
| Chris Lattner | 9cbfbc2 | 2006-01-07 01:32:28 +0000 | [diff] [blame] | 5854 | Instruction *Shift = | 
| Chris Lattner | 3e009e8 | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5855 | BinaryOperator::createShl(X, ConstantInt::get(Ty, ShiftDiff)); | 
| Chris Lattner | 9cbfbc2 | 2006-01-07 01:32:28 +0000 | [diff] [blame] | 5856 | InsertNewInstBefore(Shift, I); | 
|  | 5857 |  | 
| Reid Spencer | 5283032 | 2007-03-25 21:11:44 +0000 | [diff] [blame] | 5858 | APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt2)); | 
|  | 5859 | return BinaryOperator::createAnd(Shift, ConstantInt::get(Mask)); | 
| Chris Lattner | eb372a0 | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 5860 | } | 
| Chris Lattner | 3e009e8 | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5861 |  | 
| Chris Lattner | 83ac5ae9 | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 5862 | // (X << C1) >>u C2  --> X >>u (C2-C1) & (-1 >> C2) | 
| Chris Lattner | 3e009e8 | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5863 | if (I.getOpcode() == Instruction::LShr) { | 
|  | 5864 | assert(ShiftOp->getOpcode() == Instruction::Shl); | 
|  | 5865 | Instruction *Shift = | 
|  | 5866 | BinaryOperator::createLShr(X, ConstantInt::get(Ty, ShiftDiff)); | 
|  | 5867 | InsertNewInstBefore(Shift, I); | 
| Chris Lattner | eb372a0 | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 5868 |  | 
| Reid Spencer | 769a5a8 | 2007-03-26 17:18:58 +0000 | [diff] [blame] | 5869 | APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2)); | 
| Reid Spencer | 6274c72 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 5870 | return BinaryOperator::createAnd(Shift, ConstantInt::get(Mask)); | 
| Chris Lattner | 27cb9db | 2005-09-18 05:12:10 +0000 | [diff] [blame] | 5871 | } | 
| Chris Lattner | 3e009e8 | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5872 |  | 
|  | 5873 | // We can't handle (X << C1) >>s C2, it shifts arbitrary bits in. | 
|  | 5874 | } else { | 
|  | 5875 | assert(ShiftAmt2 < ShiftAmt1); | 
| Zhou Sheng | 56cda95 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 5876 | uint32_t ShiftDiff = ShiftAmt1-ShiftAmt2; | 
| Chris Lattner | 3e009e8 | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5877 |  | 
| Chris Lattner | 83ac5ae9 | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 5878 | // (X >>? C1) << C2 --> X >>? (C1-C2) & (-1 << C2) | 
| Chris Lattner | 3e009e8 | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5879 | if (I.getOpcode() == Instruction::Shl) { | 
|  | 5880 | assert(ShiftOp->getOpcode() == Instruction::LShr || | 
|  | 5881 | ShiftOp->getOpcode() == Instruction::AShr); | 
|  | 5882 | Instruction *Shift = | 
|  | 5883 | BinaryOperator::create(ShiftOp->getOpcode(), X, | 
|  | 5884 | ConstantInt::get(Ty, ShiftDiff)); | 
|  | 5885 | InsertNewInstBefore(Shift, I); | 
|  | 5886 |  | 
| Reid Spencer | 5283032 | 2007-03-25 21:11:44 +0000 | [diff] [blame] | 5887 | APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt2)); | 
| Reid Spencer | 6274c72 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 5888 | return BinaryOperator::createAnd(Shift, ConstantInt::get(Mask)); | 
| Chris Lattner | 3e009e8 | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5889 | } | 
|  | 5890 |  | 
| Chris Lattner | 83ac5ae9 | 2007-02-05 05:57:49 +0000 | [diff] [blame] | 5891 | // (X << C1) >>u C2  --> X << (C1-C2) & (-1 >> C2) | 
| Chris Lattner | 3e009e8 | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5892 | if (I.getOpcode() == Instruction::LShr) { | 
|  | 5893 | assert(ShiftOp->getOpcode() == Instruction::Shl); | 
|  | 5894 | Instruction *Shift = | 
|  | 5895 | BinaryOperator::createShl(X, ConstantInt::get(Ty, ShiftDiff)); | 
|  | 5896 | InsertNewInstBefore(Shift, I); | 
|  | 5897 |  | 
| Reid Spencer | 441486c | 2007-03-26 23:45:51 +0000 | [diff] [blame] | 5898 | APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2)); | 
| Reid Spencer | 6274c72 | 2007-03-23 18:46:34 +0000 | [diff] [blame] | 5899 | return BinaryOperator::createAnd(Shift, ConstantInt::get(Mask)); | 
| Chris Lattner | 3e009e8 | 2007-02-05 00:57:54 +0000 | [diff] [blame] | 5900 | } | 
|  | 5901 |  | 
|  | 5902 | // We can't handle (X << C1) >>a C2, it shifts arbitrary bits in. | 
| Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 5903 | } | 
| Chris Lattner | eb372a0 | 2006-01-06 07:52:12 +0000 | [diff] [blame] | 5904 | } | 
| Chris Lattner | f4cdbf3 | 2002-05-06 16:14:14 +0000 | [diff] [blame] | 5905 | return 0; | 
|  | 5906 | } | 
|  | 5907 |  | 
| Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 5908 |  | 
| Chris Lattner | 8f663e8 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 5909 | /// DecomposeSimpleLinearExpr - Analyze 'Val', seeing if it is a simple linear | 
|  | 5910 | /// expression.  If so, decompose it, returning some value X, such that Val is | 
|  | 5911 | /// X*Scale+Offset. | 
|  | 5912 | /// | 
|  | 5913 | static Value *DecomposeSimpleLinearExpr(Value *Val, unsigned &Scale, | 
| Jeff Cohen | 5a1c750 | 2007-04-04 16:58:57 +0000 | [diff] [blame] | 5914 | int &Offset) { | 
| Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 5915 | assert(Val->getType() == Type::Int32Ty && "Unexpected allocation size type!"); | 
| Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5916 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) { | 
| Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 5917 | Offset = CI->getZExtValue(); | 
|  | 5918 | Scale  = 1; | 
|  | 5919 | return ConstantInt::get(Type::Int32Ty, 0); | 
| Chris Lattner | 8f663e8 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 5920 | } else if (Instruction *I = dyn_cast<Instruction>(Val)) { | 
|  | 5921 | if (I->getNumOperands() == 2) { | 
| Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 5922 | if (ConstantInt *CUI = dyn_cast<ConstantInt>(I->getOperand(1))) { | 
| Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 5923 | if (I->getOpcode() == Instruction::Shl) { | 
|  | 5924 | // This is a value scaled by '1 << the shift amt'. | 
|  | 5925 | Scale = 1U << CUI->getZExtValue(); | 
|  | 5926 | Offset = 0; | 
|  | 5927 | return I->getOperand(0); | 
|  | 5928 | } else if (I->getOpcode() == Instruction::Mul) { | 
|  | 5929 | // This value is scaled by 'CUI'. | 
|  | 5930 | Scale = CUI->getZExtValue(); | 
|  | 5931 | Offset = 0; | 
|  | 5932 | return I->getOperand(0); | 
|  | 5933 | } else if (I->getOpcode() == Instruction::Add) { | 
|  | 5934 | // We have X+C.  Check to see if we really have (X*C2)+C1, | 
|  | 5935 | // where C1 is divisible by C2. | 
|  | 5936 | unsigned SubScale; | 
|  | 5937 | Value *SubVal = | 
|  | 5938 | DecomposeSimpleLinearExpr(I->getOperand(0), SubScale, Offset); | 
|  | 5939 | Offset += CUI->getZExtValue(); | 
|  | 5940 | if (SubScale > 1 && (Offset % SubScale == 0)) { | 
|  | 5941 | Scale = SubScale; | 
|  | 5942 | return SubVal; | 
| Chris Lattner | 8f663e8 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 5943 | } | 
|  | 5944 | } | 
|  | 5945 | } | 
|  | 5946 | } | 
|  | 5947 | } | 
|  | 5948 |  | 
|  | 5949 | // Otherwise, we can't look past this. | 
|  | 5950 | Scale = 1; | 
|  | 5951 | Offset = 0; | 
|  | 5952 | return Val; | 
|  | 5953 | } | 
|  | 5954 |  | 
|  | 5955 |  | 
| Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 5956 | /// PromoteCastOfAllocation - If we find a cast of an allocation instruction, | 
|  | 5957 | /// try to eliminate the cast by moving the type information into the alloc. | 
|  | 5958 | Instruction *InstCombiner::PromoteCastOfAllocation(CastInst &CI, | 
|  | 5959 | AllocationInst &AI) { | 
|  | 5960 | const PointerType *PTy = dyn_cast<PointerType>(CI.getType()); | 
| Chris Lattner | bb17180 | 2005-10-27 05:53:56 +0000 | [diff] [blame] | 5961 | if (!PTy) return 0;   // Not casting the allocation to a pointer type. | 
| Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 5962 |  | 
| Chris Lattner | ac87beb | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 5963 | // Remove any uses of AI that are dead. | 
|  | 5964 | assert(!CI.use_empty() && "Dead instructions should be removed earlier!"); | 
| Chris Lattner | 99c6cf6 | 2007-02-15 22:52:10 +0000 | [diff] [blame] | 5965 |  | 
| Chris Lattner | ac87beb | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 5966 | for (Value::use_iterator UI = AI.use_begin(), E = AI.use_end(); UI != E; ) { | 
|  | 5967 | Instruction *User = cast<Instruction>(*UI++); | 
|  | 5968 | if (isInstructionTriviallyDead(User)) { | 
|  | 5969 | while (UI != E && *UI == User) | 
|  | 5970 | ++UI; // If this instruction uses AI more than once, don't break UI. | 
|  | 5971 |  | 
| Chris Lattner | ac87beb | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 5972 | ++NumDeadInst; | 
| Bill Wendling | 5dbf43c | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 5973 | DOUT << "IC: DCE: " << *User; | 
| Chris Lattner | 51f5457 | 2007-03-02 19:59:19 +0000 | [diff] [blame] | 5974 | EraseInstFromFunction(*User); | 
| Chris Lattner | ac87beb | 2005-10-24 06:22:12 +0000 | [diff] [blame] | 5975 | } | 
|  | 5976 | } | 
|  | 5977 |  | 
| Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 5978 | // Get the type really allocated and the type casted to. | 
|  | 5979 | const Type *AllocElTy = AI.getAllocatedType(); | 
|  | 5980 | const Type *CastElTy = PTy->getElementType(); | 
|  | 5981 | if (!AllocElTy->isSized() || !CastElTy->isSized()) return 0; | 
| Chris Lattner | 355ecc0 | 2005-10-24 06:26:18 +0000 | [diff] [blame] | 5982 |  | 
| Chris Lattner | 945e437 | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 5983 | unsigned AllocElTyAlign = TD->getABITypeAlignment(AllocElTy); | 
|  | 5984 | unsigned CastElTyAlign = TD->getABITypeAlignment(CastElTy); | 
| Chris Lattner | 355ecc0 | 2005-10-24 06:26:18 +0000 | [diff] [blame] | 5985 | if (CastElTyAlign < AllocElTyAlign) return 0; | 
|  | 5986 |  | 
| Chris Lattner | 46705b2 | 2005-10-24 06:35:18 +0000 | [diff] [blame] | 5987 | // If the allocation has multiple uses, only promote it if we are strictly | 
|  | 5988 | // increasing the alignment of the resultant allocation.  If we keep it the | 
|  | 5989 | // same, we open the door to infinite loops of various kinds. | 
|  | 5990 | if (!AI.hasOneUse() && CastElTyAlign == AllocElTyAlign) return 0; | 
|  | 5991 |  | 
| Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 5992 | uint64_t AllocElTySize = TD->getTypeSize(AllocElTy); | 
|  | 5993 | uint64_t CastElTySize = TD->getTypeSize(CastElTy); | 
| Chris Lattner | bb17180 | 2005-10-27 05:53:56 +0000 | [diff] [blame] | 5994 | if (CastElTySize == 0 || AllocElTySize == 0) return 0; | 
| Chris Lattner | 355ecc0 | 2005-10-24 06:26:18 +0000 | [diff] [blame] | 5995 |  | 
| Chris Lattner | 8270c33 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 5996 | // See if we can satisfy the modulus by pulling a scale out of the array | 
|  | 5997 | // size argument. | 
| Jeff Cohen | 5a1c750 | 2007-04-04 16:58:57 +0000 | [diff] [blame] | 5998 | unsigned ArraySizeScale; | 
|  | 5999 | int ArrayOffset; | 
| Chris Lattner | 8f663e8 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 6000 | Value *NumElements = // See if the array size is a decomposable linear expr. | 
|  | 6001 | DecomposeSimpleLinearExpr(AI.getOperand(0), ArraySizeScale, ArrayOffset); | 
|  | 6002 |  | 
| Chris Lattner | 8270c33 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 6003 | // If we can now satisfy the modulus, by using a non-1 scale, we really can | 
|  | 6004 | // do the xform. | 
| Chris Lattner | 8f663e8 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 6005 | if ((AllocElTySize*ArraySizeScale) % CastElTySize != 0 || | 
|  | 6006 | (AllocElTySize*ArrayOffset   ) % CastElTySize != 0) return 0; | 
| Chris Lattner | b3ecf96 | 2005-10-27 06:12:00 +0000 | [diff] [blame] | 6007 |  | 
| Chris Lattner | 8270c33 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 6008 | unsigned Scale = (AllocElTySize*ArraySizeScale)/CastElTySize; | 
|  | 6009 | Value *Amt = 0; | 
|  | 6010 | if (Scale == 1) { | 
|  | 6011 | Amt = NumElements; | 
|  | 6012 | } else { | 
| Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6013 | // If the allocation size is constant, form a constant mul expression | 
| Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 6014 | Amt = ConstantInt::get(Type::Int32Ty, Scale); | 
|  | 6015 | if (isa<ConstantInt>(NumElements)) | 
| Zhou Sheng | 9bc8ab1 | 2007-04-02 13:45:30 +0000 | [diff] [blame] | 6016 | Amt = Multiply(cast<ConstantInt>(NumElements), cast<ConstantInt>(Amt)); | 
| Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 6017 | // otherwise multiply the amount and the number of elements | 
| Chris Lattner | 8270c33 | 2005-10-29 03:19:53 +0000 | [diff] [blame] | 6018 | else if (Scale != 1) { | 
|  | 6019 | Instruction *Tmp = BinaryOperator::createMul(Amt, NumElements, "tmp"); | 
|  | 6020 | Amt = InsertNewInstBefore(Tmp, AI); | 
| Chris Lattner | b3ecf96 | 2005-10-27 06:12:00 +0000 | [diff] [blame] | 6021 | } | 
| Chris Lattner | bb17180 | 2005-10-27 05:53:56 +0000 | [diff] [blame] | 6022 | } | 
|  | 6023 |  | 
| Jeff Cohen | 5a1c750 | 2007-04-04 16:58:57 +0000 | [diff] [blame] | 6024 | if (int Offset = (AllocElTySize*ArrayOffset)/CastElTySize) { | 
|  | 6025 | Value *Off = ConstantInt::get(Type::Int32Ty, Offset, true); | 
| Chris Lattner | 8f663e8 | 2005-10-29 04:36:15 +0000 | [diff] [blame] | 6026 | Instruction *Tmp = BinaryOperator::createAdd(Amt, Off, "tmp"); | 
|  | 6027 | Amt = InsertNewInstBefore(Tmp, AI); | 
|  | 6028 | } | 
|  | 6029 |  | 
| Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6030 | AllocationInst *New; | 
|  | 6031 | if (isa<MallocInst>(AI)) | 
| Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 6032 | New = new MallocInst(CastElTy, Amt, AI.getAlignment()); | 
| Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6033 | else | 
| Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 6034 | New = new AllocaInst(CastElTy, Amt, AI.getAlignment()); | 
| Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6035 | InsertNewInstBefore(New, AI); | 
| Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 6036 | New->takeName(&AI); | 
| Chris Lattner | 46705b2 | 2005-10-24 06:35:18 +0000 | [diff] [blame] | 6037 |  | 
|  | 6038 | // If the allocation has multiple uses, insert a cast and change all things | 
|  | 6039 | // that used it to use the new cast.  This will also hack on CI, but it will | 
|  | 6040 | // die soon. | 
|  | 6041 | if (!AI.hasOneUse()) { | 
|  | 6042 | AddUsesToWorkList(AI); | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6043 | // New is the allocation instruction, pointer typed. AI is the original | 
|  | 6044 | // allocation instruction, also pointer typed. Thus, cast to use is BitCast. | 
|  | 6045 | CastInst *NewCast = new BitCastInst(New, AI.getType(), "tmpcast"); | 
| Chris Lattner | 46705b2 | 2005-10-24 06:35:18 +0000 | [diff] [blame] | 6046 | InsertNewInstBefore(NewCast, AI); | 
|  | 6047 | AI.replaceAllUsesWith(NewCast); | 
|  | 6048 | } | 
| Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6049 | return ReplaceInstUsesWith(CI, New); | 
|  | 6050 | } | 
|  | 6051 |  | 
| Chris Lattner | 1ebbe6a | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6052 | /// CanEvaluateInDifferentType - Return true if we can take the specified value | 
| Chris Lattner | da1d04a | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6053 | /// and return it as type Ty without inserting any new casts and without | 
|  | 6054 | /// changing the computed value.  This is used by code that tries to decide | 
|  | 6055 | /// whether promoting or shrinking integer operations to wider or smaller types | 
|  | 6056 | /// will allow us to eliminate a truncate or extend. | 
|  | 6057 | /// | 
|  | 6058 | /// This is a truncation operation if Ty is smaller than V->getType(), or an | 
|  | 6059 | /// extension operation if Ty is larger. | 
|  | 6060 | static bool CanEvaluateInDifferentType(Value *V, const IntegerType *Ty, | 
| Chris Lattner | 1ebbe6a | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6061 | int &NumCastsRemoved) { | 
| Chris Lattner | da1d04a | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6062 | // We can always evaluate constants in another type. | 
|  | 6063 | if (isa<ConstantInt>(V)) | 
|  | 6064 | return true; | 
| Chris Lattner | 1ebbe6a | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6065 |  | 
|  | 6066 | Instruction *I = dyn_cast<Instruction>(V); | 
| Chris Lattner | da1d04a | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6067 | if (!I) return false; | 
|  | 6068 |  | 
|  | 6069 | const IntegerType *OrigTy = cast<IntegerType>(V->getType()); | 
| Chris Lattner | 1ebbe6a | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6070 |  | 
|  | 6071 | switch (I->getOpcode()) { | 
| Chris Lattner | da1d04a | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6072 | case Instruction::Add: | 
|  | 6073 | case Instruction::Sub: | 
| Chris Lattner | 1ebbe6a | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6074 | case Instruction::And: | 
|  | 6075 | case Instruction::Or: | 
|  | 6076 | case Instruction::Xor: | 
| Chris Lattner | da1d04a | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6077 | if (!I->hasOneUse()) return false; | 
| Chris Lattner | 1ebbe6a | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6078 | // These operators can all arbitrarily be extended or truncated. | 
|  | 6079 | return CanEvaluateInDifferentType(I->getOperand(0), Ty, NumCastsRemoved) && | 
|  | 6080 | CanEvaluateInDifferentType(I->getOperand(1), Ty, NumCastsRemoved); | 
| Chris Lattner | da1d04a | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6081 |  | 
| Chris Lattner | 960acb0 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 6082 | case Instruction::Shl: | 
| Chris Lattner | da1d04a | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6083 | if (!I->hasOneUse()) return false; | 
|  | 6084 | // If we are truncating the result of this SHL, and if it's a shift of a | 
|  | 6085 | // constant amount, we can always perform a SHL in a smaller type. | 
|  | 6086 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) { | 
| Zhou Sheng | fd28a33 | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 6087 | uint32_t BitWidth = Ty->getBitWidth(); | 
|  | 6088 | if (BitWidth < OrigTy->getBitWidth() && | 
|  | 6089 | CI->getLimitedValue(BitWidth) < BitWidth) | 
| Chris Lattner | da1d04a | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6090 | return CanEvaluateInDifferentType(I->getOperand(0), Ty,NumCastsRemoved); | 
|  | 6091 | } | 
|  | 6092 | break; | 
|  | 6093 | case Instruction::LShr: | 
|  | 6094 | if (!I->hasOneUse()) return false; | 
|  | 6095 | // If this is a truncate of a logical shr, we can truncate it to a smaller | 
|  | 6096 | // lshr iff we know that the bits we would otherwise be shifting in are | 
|  | 6097 | // already zeros. | 
|  | 6098 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) { | 
| Zhou Sheng | fd28a33 | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 6099 | uint32_t OrigBitWidth = OrigTy->getBitWidth(); | 
|  | 6100 | uint32_t BitWidth = Ty->getBitWidth(); | 
|  | 6101 | if (BitWidth < OrigBitWidth && | 
| Chris Lattner | da1d04a | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6102 | MaskedValueIsZero(I->getOperand(0), | 
| Zhou Sheng | fd28a33 | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 6103 | APInt::getHighBitsSet(OrigBitWidth, OrigBitWidth-BitWidth)) && | 
|  | 6104 | CI->getLimitedValue(BitWidth) < BitWidth) { | 
| Chris Lattner | da1d04a | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6105 | return CanEvaluateInDifferentType(I->getOperand(0), Ty, NumCastsRemoved); | 
|  | 6106 | } | 
|  | 6107 | } | 
| Chris Lattner | 960acb0 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 6108 | break; | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6109 | case Instruction::Trunc: | 
|  | 6110 | case Instruction::ZExt: | 
|  | 6111 | case Instruction::SExt: | 
| Chris Lattner | 1ebbe6a | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6112 | // If this is a cast from the destination type, we can trivially eliminate | 
|  | 6113 | // it, and this will remove a cast overall. | 
|  | 6114 | if (I->getOperand(0)->getType() == Ty) { | 
| Chris Lattner | 3fda386 | 2006-06-28 17:34:50 +0000 | [diff] [blame] | 6115 | // If the first operand is itself a cast, and is eliminable, do not count | 
|  | 6116 | // this as an eliminable cast.  We would prefer to eliminate those two | 
|  | 6117 | // casts first. | 
| Reid Spencer | de46e48 | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 6118 | if (isa<CastInst>(I->getOperand(0))) | 
| Chris Lattner | 3fda386 | 2006-06-28 17:34:50 +0000 | [diff] [blame] | 6119 | return true; | 
|  | 6120 |  | 
| Chris Lattner | 1ebbe6a | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6121 | ++NumCastsRemoved; | 
|  | 6122 | return true; | 
|  | 6123 | } | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6124 | break; | 
|  | 6125 | default: | 
| Chris Lattner | 1ebbe6a | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6126 | // TODO: Can handle more cases here. | 
|  | 6127 | break; | 
|  | 6128 | } | 
|  | 6129 |  | 
|  | 6130 | return false; | 
|  | 6131 | } | 
|  | 6132 |  | 
|  | 6133 | /// EvaluateInDifferentType - Given an expression that | 
|  | 6134 | /// CanEvaluateInDifferentType returns true for, actually insert the code to | 
|  | 6135 | /// evaluate the expression. | 
| Reid Spencer | 74a528b | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 6136 | Value *InstCombiner::EvaluateInDifferentType(Value *V, const Type *Ty, | 
| Chris Lattner | da1d04a | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6137 | bool isSigned) { | 
| Chris Lattner | 1ebbe6a | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6138 | if (Constant *C = dyn_cast<Constant>(V)) | 
| Reid Spencer | 74a528b | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 6139 | return ConstantExpr::getIntegerCast(C, Ty, isSigned /*Sext or ZExt*/); | 
| Chris Lattner | 1ebbe6a | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6140 |  | 
|  | 6141 | // Otherwise, it must be an instruction. | 
|  | 6142 | Instruction *I = cast<Instruction>(V); | 
| Chris Lattner | d0622b6 | 2006-05-20 23:14:03 +0000 | [diff] [blame] | 6143 | Instruction *Res = 0; | 
| Chris Lattner | 1ebbe6a | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6144 | switch (I->getOpcode()) { | 
| Chris Lattner | da1d04a | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6145 | case Instruction::Add: | 
|  | 6146 | case Instruction::Sub: | 
| Chris Lattner | 1ebbe6a | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6147 | case Instruction::And: | 
|  | 6148 | case Instruction::Or: | 
| Chris Lattner | da1d04a | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6149 | case Instruction::Xor: | 
| Chris Lattner | 960acb0 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 6150 | case Instruction::AShr: | 
|  | 6151 | case Instruction::LShr: | 
|  | 6152 | case Instruction::Shl: { | 
| Reid Spencer | 74a528b | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 6153 | Value *LHS = EvaluateInDifferentType(I->getOperand(0), Ty, isSigned); | 
| Chris Lattner | da1d04a | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6154 | Value *RHS = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned); | 
|  | 6155 | Res = BinaryOperator::create((Instruction::BinaryOps)I->getOpcode(), | 
|  | 6156 | LHS, RHS, I->getName()); | 
| Chris Lattner | 960acb0 | 2006-11-29 07:18:39 +0000 | [diff] [blame] | 6157 | break; | 
|  | 6158 | } | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6159 | case Instruction::Trunc: | 
|  | 6160 | case Instruction::ZExt: | 
|  | 6161 | case Instruction::SExt: | 
|  | 6162 | case Instruction::BitCast: | 
|  | 6163 | // If the source type of the cast is the type we're trying for then we can | 
|  | 6164 | // just return the source. There's no need to insert it because its not new. | 
| Chris Lattner | 1ebbe6a | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6165 | if (I->getOperand(0)->getType() == Ty) | 
|  | 6166 | return I->getOperand(0); | 
|  | 6167 |  | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6168 | // Some other kind of cast, which shouldn't happen, so just .. | 
|  | 6169 | // FALL THROUGH | 
|  | 6170 | default: | 
| Chris Lattner | 1ebbe6a | 2006-05-13 02:06:03 +0000 | [diff] [blame] | 6171 | // TODO: Can handle more cases here. | 
|  | 6172 | assert(0 && "Unreachable!"); | 
|  | 6173 | break; | 
|  | 6174 | } | 
|  | 6175 |  | 
|  | 6176 | return InsertNewInstBefore(Res, *I); | 
|  | 6177 | } | 
|  | 6178 |  | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6179 | /// @brief Implement the transforms common to all CastInst visitors. | 
|  | 6180 | Instruction *InstCombiner::commonCastTransforms(CastInst &CI) { | 
| Chris Lattner | 55d4bda | 2003-06-23 21:59:52 +0000 | [diff] [blame] | 6181 | Value *Src = CI.getOperand(0); | 
|  | 6182 |  | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6183 | // Casting undef to anything results in undef so might as just replace it and | 
|  | 6184 | // get rid of the cast. | 
| Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 6185 | if (isa<UndefValue>(Src))   // cast undef -> undef | 
|  | 6186 | return ReplaceInstUsesWith(CI, UndefValue::get(CI.getType())); | 
|  | 6187 |  | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6188 | // Many cases of "cast of a cast" are eliminable. If its eliminable we just | 
|  | 6189 | // eliminate it now. | 
| Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 6190 | if (CastInst *CSrc = dyn_cast<CastInst>(Src)) {   // A->B->C cast | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6191 | if (Instruction::CastOps opc = | 
|  | 6192 | isEliminableCastPair(CSrc, CI.getOpcode(), CI.getType(), TD)) { | 
|  | 6193 | // The first cast (CSrc) is eliminable so we need to fix up or replace | 
|  | 6194 | // the second cast (CI). CSrc will then have a good chance of being dead. | 
|  | 6195 | return CastInst::create(opc, CSrc->getOperand(0), CI.getType()); | 
| Chris Lattner | 650b6da | 2002-08-02 20:00:25 +0000 | [diff] [blame] | 6196 | } | 
|  | 6197 | } | 
| Chris Lattner | 0384165 | 2004-05-25 04:29:21 +0000 | [diff] [blame] | 6198 |  | 
| Chris Lattner | d0d5160 | 2003-06-21 23:12:02 +0000 | [diff] [blame] | 6199 | // If casting the result of a getelementptr instruction with no offset, turn | 
|  | 6200 | // this into a cast of the original pointer! | 
|  | 6201 | // | 
| Chris Lattner | 55d4bda | 2003-06-23 21:59:52 +0000 | [diff] [blame] | 6202 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Src)) { | 
| Chris Lattner | d0d5160 | 2003-06-21 23:12:02 +0000 | [diff] [blame] | 6203 | bool AllZeroOperands = true; | 
|  | 6204 | for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i) | 
|  | 6205 | if (!isa<Constant>(GEP->getOperand(i)) || | 
|  | 6206 | !cast<Constant>(GEP->getOperand(i))->isNullValue()) { | 
|  | 6207 | AllZeroOperands = false; | 
|  | 6208 | break; | 
|  | 6209 | } | 
|  | 6210 | if (AllZeroOperands) { | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6211 | // Changing the cast operand is usually not a good idea but it is safe | 
|  | 6212 | // here because the pointer operand is being replaced with another | 
|  | 6213 | // pointer operand so the opcode doesn't need to change. | 
| Chris Lattner | d0d5160 | 2003-06-21 23:12:02 +0000 | [diff] [blame] | 6214 | CI.setOperand(0, GEP->getOperand(0)); | 
|  | 6215 | return &CI; | 
|  | 6216 | } | 
|  | 6217 | } | 
| Chris Lattner | ec45a4c | 2006-11-21 17:05:13 +0000 | [diff] [blame] | 6218 |  | 
| Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 6219 | // If we are casting a malloc or alloca to a pointer to a type of the same | 
|  | 6220 | // size, rewrite the allocation instruction to allocate the "right" type. | 
| Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 6221 | if (AllocationInst *AI = dyn_cast<AllocationInst>(Src)) | 
| Chris Lattner | 216be91 | 2005-10-24 06:03:58 +0000 | [diff] [blame] | 6222 | if (Instruction *V = PromoteCastOfAllocation(CI, *AI)) | 
|  | 6223 | return V; | 
| Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 6224 |  | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6225 | // If we are casting a select then fold the cast into the select | 
| Chris Lattner | 86102b8 | 2005-01-01 16:22:27 +0000 | [diff] [blame] | 6226 | if (SelectInst *SI = dyn_cast<SelectInst>(Src)) | 
|  | 6227 | if (Instruction *NV = FoldOpIntoSelect(CI, SI, this)) | 
|  | 6228 | return NV; | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6229 |  | 
|  | 6230 | // If we are casting a PHI then fold the cast into the PHI | 
| Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 6231 | if (isa<PHINode>(Src)) | 
|  | 6232 | if (Instruction *NV = FoldOpIntoPhi(CI)) | 
|  | 6233 | return NV; | 
| Chris Lattner | b19a5c6 | 2006-04-12 18:09:35 +0000 | [diff] [blame] | 6234 |  | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6235 | return 0; | 
|  | 6236 | } | 
|  | 6237 |  | 
| Chris Lattner | da1d04a | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6238 | /// Only the TRUNC, ZEXT, SEXT, and BITCAST can both operand and result as | 
|  | 6239 | /// integer types. This function implements the common transforms for all those | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6240 | /// cases. | 
|  | 6241 | /// @brief Implement the transforms common to CastInst with integer operands | 
|  | 6242 | Instruction *InstCombiner::commonIntCastTransforms(CastInst &CI) { | 
|  | 6243 | if (Instruction *Result = commonCastTransforms(CI)) | 
|  | 6244 | return Result; | 
|  | 6245 |  | 
|  | 6246 | Value *Src = CI.getOperand(0); | 
|  | 6247 | const Type *SrcTy = Src->getType(); | 
|  | 6248 | const Type *DestTy = CI.getType(); | 
| Zhou Sheng | 56cda95 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 6249 | uint32_t SrcBitSize = SrcTy->getPrimitiveSizeInBits(); | 
|  | 6250 | uint32_t DestBitSize = DestTy->getPrimitiveSizeInBits(); | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6251 |  | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6252 | // See if we can simplify any instructions used by the LHS whose sole | 
|  | 6253 | // purpose is to compute bits we don't care about. | 
| Reid Spencer | 4154e73 | 2007-03-22 20:56:53 +0000 | [diff] [blame] | 6254 | APInt KnownZero(DestBitSize, 0), KnownOne(DestBitSize, 0); | 
|  | 6255 | if (SimplifyDemandedBits(&CI, APInt::getAllOnesValue(DestBitSize), | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6256 | KnownZero, KnownOne)) | 
|  | 6257 | return &CI; | 
|  | 6258 |  | 
|  | 6259 | // If the source isn't an instruction or has more than one use then we | 
|  | 6260 | // can't do anything more. | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6261 | Instruction *SrcI = dyn_cast<Instruction>(Src); | 
|  | 6262 | if (!SrcI || !Src->hasOneUse()) | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6263 | return 0; | 
|  | 6264 |  | 
| Chris Lattner | da1d04a | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6265 | // Attempt to propagate the cast into the instruction for int->int casts. | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6266 | int NumCastsRemoved = 0; | 
| Chris Lattner | da1d04a | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6267 | if (!isa<BitCastInst>(CI) && | 
|  | 6268 | CanEvaluateInDifferentType(SrcI, cast<IntegerType>(DestTy), | 
|  | 6269 | NumCastsRemoved)) { | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6270 | // If this cast is a truncate, evaluting in a different type always | 
|  | 6271 | // eliminates the cast, so it is always a win.  If this is a noop-cast | 
|  | 6272 | // this just removes a noop cast which isn't pointful, but simplifies | 
|  | 6273 | // the code.  If this is a zero-extension, we need to do an AND to | 
|  | 6274 | // maintain the clear top-part of the computation, so we require that | 
|  | 6275 | // the input have eliminated at least one cast.  If this is a sign | 
|  | 6276 | // extension, we insert two new casts (to do the extension) so we | 
|  | 6277 | // require that two casts have been eliminated. | 
| Chris Lattner | da1d04a | 2007-03-03 05:27:34 +0000 | [diff] [blame] | 6278 | bool DoXForm; | 
|  | 6279 | switch (CI.getOpcode()) { | 
|  | 6280 | default: | 
|  | 6281 | // All the others use floating point so we shouldn't actually | 
|  | 6282 | // get here because of the check above. | 
|  | 6283 | assert(0 && "Unknown cast type"); | 
|  | 6284 | case Instruction::Trunc: | 
|  | 6285 | DoXForm = true; | 
|  | 6286 | break; | 
|  | 6287 | case Instruction::ZExt: | 
|  | 6288 | DoXForm = NumCastsRemoved >= 1; | 
|  | 6289 | break; | 
|  | 6290 | case Instruction::SExt: | 
|  | 6291 | DoXForm = NumCastsRemoved >= 2; | 
|  | 6292 | break; | 
|  | 6293 | case Instruction::BitCast: | 
|  | 6294 | DoXForm = false; | 
|  | 6295 | break; | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6296 | } | 
|  | 6297 |  | 
|  | 6298 | if (DoXForm) { | 
| Reid Spencer | 74a528b | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 6299 | Value *Res = EvaluateInDifferentType(SrcI, DestTy, | 
|  | 6300 | CI.getOpcode() == Instruction::SExt); | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6301 | assert(Res->getType() == DestTy); | 
|  | 6302 | switch (CI.getOpcode()) { | 
|  | 6303 | default: assert(0 && "Unknown cast type!"); | 
|  | 6304 | case Instruction::Trunc: | 
|  | 6305 | case Instruction::BitCast: | 
|  | 6306 | // Just replace this cast with the result. | 
|  | 6307 | return ReplaceInstUsesWith(CI, Res); | 
|  | 6308 | case Instruction::ZExt: { | 
|  | 6309 | // We need to emit an AND to clear the high bits. | 
|  | 6310 | assert(SrcBitSize < DestBitSize && "Not a zext?"); | 
| Chris Lattner | 9d5aace | 2007-04-02 05:48:58 +0000 | [diff] [blame] | 6311 | Constant *C = ConstantInt::get(APInt::getLowBitsSet(DestBitSize, | 
|  | 6312 | SrcBitSize)); | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6313 | return BinaryOperator::createAnd(Res, C); | 
|  | 6314 | } | 
|  | 6315 | case Instruction::SExt: | 
|  | 6316 | // We need to emit a cast to truncate, then a cast to sext. | 
|  | 6317 | return CastInst::create(Instruction::SExt, | 
| Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 6318 | InsertCastBefore(Instruction::Trunc, Res, Src->getType(), | 
|  | 6319 | CI), DestTy); | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6320 | } | 
|  | 6321 | } | 
|  | 6322 | } | 
|  | 6323 |  | 
|  | 6324 | Value *Op0 = SrcI->getNumOperands() > 0 ? SrcI->getOperand(0) : 0; | 
|  | 6325 | Value *Op1 = SrcI->getNumOperands() > 1 ? SrcI->getOperand(1) : 0; | 
|  | 6326 |  | 
|  | 6327 | switch (SrcI->getOpcode()) { | 
|  | 6328 | case Instruction::Add: | 
|  | 6329 | case Instruction::Mul: | 
|  | 6330 | case Instruction::And: | 
|  | 6331 | case Instruction::Or: | 
|  | 6332 | case Instruction::Xor: | 
| Chris Lattner | a74deaf | 2007-04-03 17:43:25 +0000 | [diff] [blame] | 6333 | // If we are discarding information, rewrite. | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6334 | if (DestBitSize <= SrcBitSize && DestBitSize != 1) { | 
|  | 6335 | // Don't insert two casts if they cannot be eliminated.  We allow | 
|  | 6336 | // two casts to be inserted if the sizes are the same.  This could | 
|  | 6337 | // only be converting signedness, which is a noop. | 
|  | 6338 | if (DestBitSize == SrcBitSize || | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6339 | !ValueRequiresCast(CI.getOpcode(), Op1, DestTy,TD) || | 
|  | 6340 | !ValueRequiresCast(CI.getOpcode(), Op0, DestTy, TD)) { | 
| Reid Spencer | 2a499b0 | 2006-12-13 17:19:09 +0000 | [diff] [blame] | 6341 | Instruction::CastOps opcode = CI.getOpcode(); | 
| Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 6342 | Value *Op0c = InsertOperandCastBefore(opcode, Op0, DestTy, SrcI); | 
|  | 6343 | Value *Op1c = InsertOperandCastBefore(opcode, Op1, DestTy, SrcI); | 
|  | 6344 | return BinaryOperator::create( | 
|  | 6345 | cast<BinaryOperator>(SrcI)->getOpcode(), Op0c, Op1c); | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6346 | } | 
|  | 6347 | } | 
|  | 6348 |  | 
|  | 6349 | // cast (xor bool X, true) to int  --> xor (cast bool X to int), 1 | 
|  | 6350 | if (isa<ZExtInst>(CI) && SrcBitSize == 1 && | 
|  | 6351 | SrcI->getOpcode() == Instruction::Xor && | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 6352 | Op1 == ConstantInt::getTrue() && | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6353 | (!Op0->hasOneUse() || !isa<CmpInst>(Op0))) { | 
| Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 6354 | Value *New = InsertOperandCastBefore(Instruction::ZExt, Op0, DestTy, &CI); | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6355 | return BinaryOperator::createXor(New, ConstantInt::get(CI.getType(), 1)); | 
|  | 6356 | } | 
|  | 6357 | break; | 
|  | 6358 | case Instruction::SDiv: | 
|  | 6359 | case Instruction::UDiv: | 
|  | 6360 | case Instruction::SRem: | 
|  | 6361 | case Instruction::URem: | 
|  | 6362 | // If we are just changing the sign, rewrite. | 
|  | 6363 | if (DestBitSize == SrcBitSize) { | 
|  | 6364 | // Don't insert two casts if they cannot be eliminated.  We allow | 
|  | 6365 | // two casts to be inserted if the sizes are the same.  This could | 
|  | 6366 | // only be converting signedness, which is a noop. | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6367 | if (!ValueRequiresCast(CI.getOpcode(), Op1, DestTy, TD) || | 
|  | 6368 | !ValueRequiresCast(CI.getOpcode(), Op0, DestTy, TD)) { | 
| Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 6369 | Value *Op0c = InsertOperandCastBefore(Instruction::BitCast, | 
|  | 6370 | Op0, DestTy, SrcI); | 
|  | 6371 | Value *Op1c = InsertOperandCastBefore(Instruction::BitCast, | 
|  | 6372 | Op1, DestTy, SrcI); | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6373 | return BinaryOperator::create( | 
|  | 6374 | cast<BinaryOperator>(SrcI)->getOpcode(), Op0c, Op1c); | 
|  | 6375 | } | 
|  | 6376 | } | 
|  | 6377 | break; | 
|  | 6378 |  | 
|  | 6379 | case Instruction::Shl: | 
|  | 6380 | // Allow changing the sign of the source operand.  Do not allow | 
|  | 6381 | // changing the size of the shift, UNLESS the shift amount is a | 
|  | 6382 | // constant.  We must not change variable sized shifts to a smaller | 
|  | 6383 | // size, because it is undefined to shift more bits out than exist | 
|  | 6384 | // in the value. | 
|  | 6385 | if (DestBitSize == SrcBitSize || | 
|  | 6386 | (DestBitSize < SrcBitSize && isa<Constant>(Op1))) { | 
| Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 6387 | Instruction::CastOps opcode = (DestBitSize == SrcBitSize ? | 
|  | 6388 | Instruction::BitCast : Instruction::Trunc); | 
|  | 6389 | Value *Op0c = InsertOperandCastBefore(opcode, Op0, DestTy, SrcI); | 
| Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6390 | Value *Op1c = InsertOperandCastBefore(opcode, Op1, DestTy, SrcI); | 
| Reid Spencer | 0d5f923 | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 6391 | return BinaryOperator::createShl(Op0c, Op1c); | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6392 | } | 
|  | 6393 | break; | 
|  | 6394 | case Instruction::AShr: | 
|  | 6395 | // If this is a signed shr, and if all bits shifted in are about to be | 
|  | 6396 | // truncated off, turn it into an unsigned shr to allow greater | 
|  | 6397 | // simplifications. | 
|  | 6398 | if (DestBitSize < SrcBitSize && | 
|  | 6399 | isa<ConstantInt>(Op1)) { | 
| Zhou Sheng | fd28a33 | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 6400 | uint32_t ShiftAmt = cast<ConstantInt>(Op1)->getLimitedValue(SrcBitSize); | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6401 | if (SrcBitSize > ShiftAmt && SrcBitSize-ShiftAmt >= DestBitSize) { | 
|  | 6402 | // Insert the new logical shift right. | 
| Reid Spencer | 0d5f923 | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 6403 | return BinaryOperator::createLShr(Op0, Op1); | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6404 | } | 
|  | 6405 | } | 
|  | 6406 | break; | 
|  | 6407 |  | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6408 | case Instruction::ICmp: | 
|  | 6409 | // If we are just checking for a icmp eq of a single bit and casting it | 
|  | 6410 | // to an integer, then shift the bit to the appropriate place and then | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6411 | // cast to integer to avoid the comparison. | 
|  | 6412 | if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) { | 
| Zhou Sheng | 150f3bb | 2007-04-01 17:13:37 +0000 | [diff] [blame] | 6413 | const APInt& Op1CV = Op1C->getValue(); | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6414 | // cast (X == 0) to int --> X^1      iff X has only the low bit set. | 
|  | 6415 | // cast (X == 0) to int --> (X>>1)^1 iff X has only the 2nd bit set. | 
|  | 6416 | // cast (X == 1) to int --> X        iff X has only the low bit set. | 
|  | 6417 | // cast (X == 2) to int --> X>>1     iff X has only the 2nd bit set. | 
|  | 6418 | // cast (X != 0) to int --> X        iff X has only the low bit set. | 
|  | 6419 | // cast (X != 0) to int --> X>>1     iff X has only the 2nd bit set. | 
|  | 6420 | // cast (X != 1) to int --> X^1      iff X has only the low bit set. | 
|  | 6421 | // cast (X != 2) to int --> (X>>1)^1 iff X has only the 2nd bit set. | 
| Reid Spencer | 4154e73 | 2007-03-22 20:56:53 +0000 | [diff] [blame] | 6422 | if (Op1CV == 0 || Op1CV.isPowerOf2()) { | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6423 | // If Op1C some other power of two, convert: | 
| Reid Spencer | 4154e73 | 2007-03-22 20:56:53 +0000 | [diff] [blame] | 6424 | uint32_t BitWidth = Op1C->getType()->getBitWidth(); | 
|  | 6425 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); | 
|  | 6426 | APInt TypeMask(APInt::getAllOnesValue(BitWidth)); | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6427 | ComputeMaskedBits(Op0, TypeMask, KnownZero, KnownOne); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6428 |  | 
|  | 6429 | // This only works for EQ and NE | 
|  | 6430 | ICmpInst::Predicate pred = cast<ICmpInst>(SrcI)->getPredicate(); | 
|  | 6431 | if (pred != ICmpInst::ICMP_NE && pred != ICmpInst::ICMP_EQ) | 
|  | 6432 | break; | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6433 |  | 
| Zhou Sheng | 0900993e | 2007-03-23 03:13:21 +0000 | [diff] [blame] | 6434 | APInt KnownZeroMask(KnownZero ^ TypeMask); | 
|  | 6435 | if (KnownZeroMask.isPowerOf2()) { // Exactly 1 possible 1? | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6436 | bool isNE = pred == ICmpInst::ICMP_NE; | 
| Zhou Sheng | 0900993e | 2007-03-23 03:13:21 +0000 | [diff] [blame] | 6437 | if (Op1CV != 0 && (Op1CV != KnownZeroMask)) { | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6438 | // (X&4) == 2 --> false | 
|  | 6439 | // (X&4) != 2 --> true | 
| Reid Spencer | cddc9df | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 6440 | Constant *Res = ConstantInt::get(Type::Int1Ty, isNE); | 
| Reid Spencer | bb65ebf | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 6441 | Res = ConstantExpr::getZExt(Res, CI.getType()); | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6442 | return ReplaceInstUsesWith(CI, Res); | 
|  | 6443 | } | 
|  | 6444 |  | 
| Zhou Sheng | 56cda95 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 6445 | uint32_t ShiftAmt = KnownZeroMask.logBase2(); | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6446 | Value *In = Op0; | 
|  | 6447 | if (ShiftAmt) { | 
|  | 6448 | // Perform a logical shr by shiftamt. | 
|  | 6449 | // Insert the shift to put the result in the low bit. | 
|  | 6450 | In = InsertNewInstBefore( | 
| Reid Spencer | 0d5f923 | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 6451 | BinaryOperator::createLShr(In, | 
| Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6452 | ConstantInt::get(In->getType(), ShiftAmt), | 
|  | 6453 | In->getName()+".lobit"), CI); | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6454 | } | 
|  | 6455 |  | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6456 | if ((Op1CV != 0) == isNE) { // Toggle the low bit. | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6457 | Constant *One = ConstantInt::get(In->getType(), 1); | 
|  | 6458 | In = BinaryOperator::createXor(In, One, "tmp"); | 
|  | 6459 | InsertNewInstBefore(cast<Instruction>(In), CI); | 
|  | 6460 | } | 
|  | 6461 |  | 
|  | 6462 | if (CI.getType() == In->getType()) | 
|  | 6463 | return ReplaceInstUsesWith(CI, In); | 
|  | 6464 | else | 
| Reid Spencer | bb65ebf | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 6465 | return CastInst::createIntegerCast(In, CI.getType(), false/*ZExt*/); | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6466 | } | 
|  | 6467 | } | 
|  | 6468 | } | 
|  | 6469 | break; | 
|  | 6470 | } | 
|  | 6471 | return 0; | 
|  | 6472 | } | 
|  | 6473 |  | 
|  | 6474 | Instruction *InstCombiner::visitTrunc(CastInst &CI) { | 
| Chris Lattner | d747f01 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 6475 | if (Instruction *Result = commonIntCastTransforms(CI)) | 
|  | 6476 | return Result; | 
|  | 6477 |  | 
|  | 6478 | Value *Src = CI.getOperand(0); | 
|  | 6479 | const Type *Ty = CI.getType(); | 
| Zhou Sheng | 56cda95 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 6480 | uint32_t DestBitWidth = Ty->getPrimitiveSizeInBits(); | 
|  | 6481 | uint32_t SrcBitWidth = cast<IntegerType>(Src->getType())->getBitWidth(); | 
| Chris Lattner | d747f01 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 6482 |  | 
|  | 6483 | if (Instruction *SrcI = dyn_cast<Instruction>(Src)) { | 
|  | 6484 | switch (SrcI->getOpcode()) { | 
|  | 6485 | default: break; | 
|  | 6486 | case Instruction::LShr: | 
|  | 6487 | // We can shrink lshr to something smaller if we know the bits shifted in | 
|  | 6488 | // are already zeros. | 
|  | 6489 | if (ConstantInt *ShAmtV = dyn_cast<ConstantInt>(SrcI->getOperand(1))) { | 
| Zhou Sheng | fd28a33 | 2007-03-30 17:20:39 +0000 | [diff] [blame] | 6490 | uint32_t ShAmt = ShAmtV->getLimitedValue(SrcBitWidth); | 
| Chris Lattner | d747f01 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 6491 |  | 
|  | 6492 | // Get a mask for the bits shifting in. | 
| Zhou Sheng | 2777a31 | 2007-03-28 09:19:01 +0000 | [diff] [blame] | 6493 | APInt Mask(APInt::getLowBitsSet(SrcBitWidth, ShAmt).shl(DestBitWidth)); | 
| Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 6494 | Value* SrcIOp0 = SrcI->getOperand(0); | 
|  | 6495 | if (SrcI->hasOneUse() && MaskedValueIsZero(SrcIOp0, Mask)) { | 
| Chris Lattner | d747f01 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 6496 | if (ShAmt >= DestBitWidth)        // All zeros. | 
|  | 6497 | return ReplaceInstUsesWith(CI, Constant::getNullValue(Ty)); | 
|  | 6498 |  | 
|  | 6499 | // Okay, we can shrink this.  Truncate the input, then return a new | 
|  | 6500 | // shift. | 
| Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6501 | Value *V1 = InsertCastBefore(Instruction::Trunc, SrcIOp0, Ty, CI); | 
|  | 6502 | Value *V2 = InsertCastBefore(Instruction::Trunc, SrcI->getOperand(1), | 
|  | 6503 | Ty, CI); | 
| Reid Spencer | 0d5f923 | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 6504 | return BinaryOperator::createLShr(V1, V2); | 
| Chris Lattner | d747f01 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 6505 | } | 
| Chris Lattner | c209b58 | 2006-12-05 01:26:29 +0000 | [diff] [blame] | 6506 | } else {     // This is a variable shr. | 
|  | 6507 |  | 
|  | 6508 | // Turn 'trunc (lshr X, Y) to bool' into '(X & (1 << Y)) != 0'.  This is | 
|  | 6509 | // more LLVM instructions, but allows '1 << Y' to be hoisted if | 
|  | 6510 | // loop-invariant and CSE'd. | 
| Reid Spencer | 542964f | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 6511 | if (CI.getType() == Type::Int1Ty && SrcI->hasOneUse()) { | 
| Chris Lattner | c209b58 | 2006-12-05 01:26:29 +0000 | [diff] [blame] | 6512 | Value *One = ConstantInt::get(SrcI->getType(), 1); | 
|  | 6513 |  | 
| Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6514 | Value *V = InsertNewInstBefore( | 
| Reid Spencer | 0d5f923 | 2007-02-02 14:08:20 +0000 | [diff] [blame] | 6515 | BinaryOperator::createShl(One, SrcI->getOperand(1), | 
| Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6516 | "tmp"), CI); | 
| Chris Lattner | c209b58 | 2006-12-05 01:26:29 +0000 | [diff] [blame] | 6517 | V = InsertNewInstBefore(BinaryOperator::createAnd(V, | 
|  | 6518 | SrcI->getOperand(0), | 
|  | 6519 | "tmp"), CI); | 
|  | 6520 | Value *Zero = Constant::getNullValue(V->getType()); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6521 | return new ICmpInst(ICmpInst::ICMP_NE, V, Zero); | 
| Chris Lattner | c209b58 | 2006-12-05 01:26:29 +0000 | [diff] [blame] | 6522 | } | 
| Chris Lattner | d747f01 | 2006-11-29 07:04:07 +0000 | [diff] [blame] | 6523 | } | 
|  | 6524 | break; | 
|  | 6525 | } | 
|  | 6526 | } | 
|  | 6527 |  | 
|  | 6528 | return 0; | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6529 | } | 
|  | 6530 |  | 
|  | 6531 | Instruction *InstCombiner::visitZExt(CastInst &CI) { | 
|  | 6532 | // If one of the common conversion will work .. | 
|  | 6533 | if (Instruction *Result = commonIntCastTransforms(CI)) | 
|  | 6534 | return Result; | 
|  | 6535 |  | 
|  | 6536 | Value *Src = CI.getOperand(0); | 
|  | 6537 |  | 
|  | 6538 | // If this is a cast of a cast | 
|  | 6539 | if (CastInst *CSrc = dyn_cast<CastInst>(Src)) {   // A->B->C cast | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6540 | // If this is a TRUNC followed by a ZEXT then we are dealing with integral | 
|  | 6541 | // types and if the sizes are just right we can convert this into a logical | 
|  | 6542 | // 'and' which will be much cheaper than the pair of casts. | 
|  | 6543 | if (isa<TruncInst>(CSrc)) { | 
|  | 6544 | // Get the sizes of the types involved | 
|  | 6545 | Value *A = CSrc->getOperand(0); | 
| Zhou Sheng | 56cda95 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 6546 | uint32_t SrcSize = A->getType()->getPrimitiveSizeInBits(); | 
|  | 6547 | uint32_t MidSize = CSrc->getType()->getPrimitiveSizeInBits(); | 
|  | 6548 | uint32_t DstSize = CI.getType()->getPrimitiveSizeInBits(); | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6549 | // If we're actually extending zero bits and the trunc is a no-op | 
|  | 6550 | if (MidSize < DstSize && SrcSize == DstSize) { | 
|  | 6551 | // Replace both of the casts with an And of the type mask. | 
| Zhou Sheng | 2777a31 | 2007-03-28 09:19:01 +0000 | [diff] [blame] | 6552 | APInt AndValue(APInt::getLowBitsSet(SrcSize, MidSize)); | 
| Reid Spencer | 4154e73 | 2007-03-22 20:56:53 +0000 | [diff] [blame] | 6553 | Constant *AndConst = ConstantInt::get(AndValue); | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6554 | Instruction *And = | 
|  | 6555 | BinaryOperator::createAnd(CSrc->getOperand(0), AndConst); | 
|  | 6556 | // Unfortunately, if the type changed, we need to cast it back. | 
|  | 6557 | if (And->getType() != CI.getType()) { | 
|  | 6558 | And->setName(CSrc->getName()+".mask"); | 
|  | 6559 | InsertNewInstBefore(And, CI); | 
| Reid Spencer | bb65ebf | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 6560 | And = CastInst::createIntegerCast(And, CI.getType(), false/*ZExt*/); | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6561 | } | 
|  | 6562 | return And; | 
|  | 6563 | } | 
|  | 6564 | } | 
|  | 6565 | } | 
|  | 6566 |  | 
|  | 6567 | return 0; | 
|  | 6568 | } | 
|  | 6569 |  | 
|  | 6570 | Instruction *InstCombiner::visitSExt(CastInst &CI) { | 
|  | 6571 | return commonIntCastTransforms(CI); | 
|  | 6572 | } | 
|  | 6573 |  | 
|  | 6574 | Instruction *InstCombiner::visitFPTrunc(CastInst &CI) { | 
|  | 6575 | return commonCastTransforms(CI); | 
|  | 6576 | } | 
|  | 6577 |  | 
|  | 6578 | Instruction *InstCombiner::visitFPExt(CastInst &CI) { | 
|  | 6579 | return commonCastTransforms(CI); | 
|  | 6580 | } | 
|  | 6581 |  | 
|  | 6582 | Instruction *InstCombiner::visitFPToUI(CastInst &CI) { | 
| Reid Spencer | ad05ee9 | 2006-11-30 23:13:36 +0000 | [diff] [blame] | 6583 | return commonCastTransforms(CI); | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6584 | } | 
|  | 6585 |  | 
|  | 6586 | Instruction *InstCombiner::visitFPToSI(CastInst &CI) { | 
| Reid Spencer | ad05ee9 | 2006-11-30 23:13:36 +0000 | [diff] [blame] | 6587 | return commonCastTransforms(CI); | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6588 | } | 
|  | 6589 |  | 
|  | 6590 | Instruction *InstCombiner::visitUIToFP(CastInst &CI) { | 
|  | 6591 | return commonCastTransforms(CI); | 
|  | 6592 | } | 
|  | 6593 |  | 
|  | 6594 | Instruction *InstCombiner::visitSIToFP(CastInst &CI) { | 
|  | 6595 | return commonCastTransforms(CI); | 
|  | 6596 | } | 
|  | 6597 |  | 
|  | 6598 | Instruction *InstCombiner::visitPtrToInt(CastInst &CI) { | 
| Reid Spencer | ad05ee9 | 2006-11-30 23:13:36 +0000 | [diff] [blame] | 6599 | return commonCastTransforms(CI); | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6600 | } | 
|  | 6601 |  | 
|  | 6602 | Instruction *InstCombiner::visitIntToPtr(CastInst &CI) { | 
|  | 6603 | return commonCastTransforms(CI); | 
|  | 6604 | } | 
|  | 6605 |  | 
|  | 6606 | Instruction *InstCombiner::visitBitCast(CastInst &CI) { | 
|  | 6607 |  | 
|  | 6608 | // If the operands are integer typed then apply the integer transforms, | 
|  | 6609 | // otherwise just apply the common ones. | 
|  | 6610 | Value *Src = CI.getOperand(0); | 
|  | 6611 | const Type *SrcTy = Src->getType(); | 
|  | 6612 | const Type *DestTy = CI.getType(); | 
|  | 6613 |  | 
| Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 6614 | if (SrcTy->isInteger() && DestTy->isInteger()) { | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6615 | if (Instruction *Result = commonIntCastTransforms(CI)) | 
|  | 6616 | return Result; | 
|  | 6617 | } else { | 
|  | 6618 | if (Instruction *Result = commonCastTransforms(CI)) | 
|  | 6619 | return Result; | 
|  | 6620 | } | 
|  | 6621 |  | 
|  | 6622 |  | 
|  | 6623 | // Get rid of casts from one type to the same type. These are useless and can | 
|  | 6624 | // be replaced by the operand. | 
|  | 6625 | if (DestTy == Src->getType()) | 
|  | 6626 | return ReplaceInstUsesWith(CI, Src); | 
|  | 6627 |  | 
| Chris Lattner | b19a5c6 | 2006-04-12 18:09:35 +0000 | [diff] [blame] | 6628 | // If the source and destination are pointers, and this cast is equivalent to | 
|  | 6629 | // a getelementptr X, 0, 0, 0...  turn it into the appropriate getelementptr. | 
|  | 6630 | // This can enhance SROA and other transforms that want type-safe pointers. | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6631 | if (const PointerType *DstPTy = dyn_cast<PointerType>(DestTy)) { | 
|  | 6632 | if (const PointerType *SrcPTy = dyn_cast<PointerType>(SrcTy)) { | 
|  | 6633 | const Type *DstElTy = DstPTy->getElementType(); | 
|  | 6634 | const Type *SrcElTy = SrcPTy->getElementType(); | 
| Chris Lattner | b19a5c6 | 2006-04-12 18:09:35 +0000 | [diff] [blame] | 6635 |  | 
| Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 6636 | Constant *ZeroUInt = Constant::getNullValue(Type::Int32Ty); | 
| Chris Lattner | b19a5c6 | 2006-04-12 18:09:35 +0000 | [diff] [blame] | 6637 | unsigned NumZeros = 0; | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6638 | while (SrcElTy != DstElTy && | 
|  | 6639 | isa<CompositeType>(SrcElTy) && !isa<PointerType>(SrcElTy) && | 
|  | 6640 | SrcElTy->getNumContainedTypes() /* not "{}" */) { | 
|  | 6641 | SrcElTy = cast<CompositeType>(SrcElTy)->getTypeAtIndex(ZeroUInt); | 
| Chris Lattner | b19a5c6 | 2006-04-12 18:09:35 +0000 | [diff] [blame] | 6642 | ++NumZeros; | 
|  | 6643 | } | 
| Chris Lattner | 6a4adcd | 2004-09-29 05:07:12 +0000 | [diff] [blame] | 6644 |  | 
| Chris Lattner | b19a5c6 | 2006-04-12 18:09:35 +0000 | [diff] [blame] | 6645 | // If we found a path from the src to dest, create the getelementptr now. | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6646 | if (SrcElTy == DstElTy) { | 
| Chris Lattner | 416a893 | 2007-01-31 20:08:52 +0000 | [diff] [blame] | 6647 | SmallVector<Value*, 8> Idxs(NumZeros+1, ZeroUInt); | 
|  | 6648 | return new GetElementPtrInst(Src, &Idxs[0], Idxs.size()); | 
| Chris Lattner | b19a5c6 | 2006-04-12 18:09:35 +0000 | [diff] [blame] | 6649 | } | 
|  | 6650 | } | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6651 | } | 
| Chris Lattner | dfae8be | 2003-07-24 17:35:25 +0000 | [diff] [blame] | 6652 |  | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6653 | if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(Src)) { | 
|  | 6654 | if (SVI->hasOneUse()) { | 
|  | 6655 | // Okay, we have (bitconvert (shuffle ..)).  Check to see if this is | 
|  | 6656 | // a bitconvert to a vector with the same # elts. | 
| Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 6657 | if (isa<VectorType>(DestTy) && | 
|  | 6658 | cast<VectorType>(DestTy)->getNumElements() == | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6659 | SVI->getType()->getNumElements()) { | 
|  | 6660 | CastInst *Tmp; | 
|  | 6661 | // If either of the operands is a cast from CI.getType(), then | 
|  | 6662 | // evaluating the shuffle in the casted destination's type will allow | 
|  | 6663 | // us to eliminate at least one cast. | 
|  | 6664 | if (((Tmp = dyn_cast<CastInst>(SVI->getOperand(0))) && | 
|  | 6665 | Tmp->getOperand(0)->getType() == DestTy) || | 
|  | 6666 | ((Tmp = dyn_cast<CastInst>(SVI->getOperand(1))) && | 
|  | 6667 | Tmp->getOperand(0)->getType() == DestTy)) { | 
| Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 6668 | Value *LHS = InsertOperandCastBefore(Instruction::BitCast, | 
|  | 6669 | SVI->getOperand(0), DestTy, &CI); | 
|  | 6670 | Value *RHS = InsertOperandCastBefore(Instruction::BitCast, | 
|  | 6671 | SVI->getOperand(1), DestTy, &CI); | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6672 | // Return a new shuffle vector.  Use the same element ID's, as we | 
|  | 6673 | // know the vector types match #elts. | 
|  | 6674 | return new ShuffleVectorInst(LHS, RHS, SVI->getOperand(2)); | 
| Chris Lattner | 99155be | 2006-05-25 23:24:33 +0000 | [diff] [blame] | 6675 | } | 
|  | 6676 | } | 
|  | 6677 | } | 
|  | 6678 | } | 
| Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 6679 | return 0; | 
| Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 6680 | } | 
|  | 6681 |  | 
| Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 6682 | /// GetSelectFoldableOperands - We want to turn code that looks like this: | 
|  | 6683 | ///   %C = or %A, %B | 
|  | 6684 | ///   %D = select %cond, %C, %A | 
|  | 6685 | /// into: | 
|  | 6686 | ///   %C = select %cond, %B, 0 | 
|  | 6687 | ///   %D = or %A, %C | 
|  | 6688 | /// | 
|  | 6689 | /// Assuming that the specified instruction is an operand to the select, return | 
|  | 6690 | /// a bitmask indicating which operands of this instruction are foldable if they | 
|  | 6691 | /// equal the other incoming value of the select. | 
|  | 6692 | /// | 
|  | 6693 | static unsigned GetSelectFoldableOperands(Instruction *I) { | 
|  | 6694 | switch (I->getOpcode()) { | 
|  | 6695 | case Instruction::Add: | 
|  | 6696 | case Instruction::Mul: | 
|  | 6697 | case Instruction::And: | 
|  | 6698 | case Instruction::Or: | 
|  | 6699 | case Instruction::Xor: | 
|  | 6700 | return 3;              // Can fold through either operand. | 
|  | 6701 | case Instruction::Sub:   // Can only fold on the amount subtracted. | 
|  | 6702 | case Instruction::Shl:   // Can only fold on the shift amount. | 
| Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 6703 | case Instruction::LShr: | 
|  | 6704 | case Instruction::AShr: | 
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 6705 | return 1; | 
| Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 6706 | default: | 
|  | 6707 | return 0;              // Cannot fold | 
|  | 6708 | } | 
|  | 6709 | } | 
|  | 6710 |  | 
|  | 6711 | /// GetSelectFoldableConstant - For the same transformation as the previous | 
|  | 6712 | /// function, return the identity constant that goes into the select. | 
|  | 6713 | static Constant *GetSelectFoldableConstant(Instruction *I) { | 
|  | 6714 | switch (I->getOpcode()) { | 
|  | 6715 | default: assert(0 && "This cannot happen!"); abort(); | 
|  | 6716 | case Instruction::Add: | 
|  | 6717 | case Instruction::Sub: | 
|  | 6718 | case Instruction::Or: | 
|  | 6719 | case Instruction::Xor: | 
| Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 6720 | case Instruction::Shl: | 
| Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 6721 | case Instruction::LShr: | 
|  | 6722 | case Instruction::AShr: | 
| Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6723 | return Constant::getNullValue(I->getType()); | 
| Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 6724 | case Instruction::And: | 
|  | 6725 | return ConstantInt::getAllOnesValue(I->getType()); | 
|  | 6726 | case Instruction::Mul: | 
|  | 6727 | return ConstantInt::get(I->getType(), 1); | 
|  | 6728 | } | 
|  | 6729 | } | 
|  | 6730 |  | 
| Chris Lattner | 411336f | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 6731 | /// FoldSelectOpOp - Here we have (select c, TI, FI), and we know that TI and FI | 
|  | 6732 | /// have the same opcode and only one use each.  Try to simplify this. | 
|  | 6733 | Instruction *InstCombiner::FoldSelectOpOp(SelectInst &SI, Instruction *TI, | 
|  | 6734 | Instruction *FI) { | 
|  | 6735 | if (TI->getNumOperands() == 1) { | 
|  | 6736 | // If this is a non-volatile load or a cast from the same type, | 
|  | 6737 | // merge. | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6738 | if (TI->isCast()) { | 
| Chris Lattner | 411336f | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 6739 | if (TI->getOperand(0)->getType() != FI->getOperand(0)->getType()) | 
|  | 6740 | return 0; | 
|  | 6741 | } else { | 
|  | 6742 | return 0;  // unknown unary op. | 
|  | 6743 | } | 
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 6744 |  | 
| Chris Lattner | 411336f | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 6745 | // Fold this by inserting a select from the input values. | 
|  | 6746 | SelectInst *NewSI = new SelectInst(SI.getCondition(), TI->getOperand(0), | 
|  | 6747 | FI->getOperand(0), SI.getName()+".v"); | 
|  | 6748 | InsertNewInstBefore(NewSI, SI); | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6749 | return CastInst::create(Instruction::CastOps(TI->getOpcode()), NewSI, | 
|  | 6750 | TI->getType()); | 
| Chris Lattner | 411336f | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 6751 | } | 
|  | 6752 |  | 
| Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6753 | // Only handle binary operators here. | 
|  | 6754 | if (!isa<BinaryOperator>(TI)) | 
| Chris Lattner | 411336f | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 6755 | return 0; | 
|  | 6756 |  | 
|  | 6757 | // Figure out if the operations have any operands in common. | 
|  | 6758 | Value *MatchOp, *OtherOpT, *OtherOpF; | 
|  | 6759 | bool MatchIsOpZero; | 
|  | 6760 | if (TI->getOperand(0) == FI->getOperand(0)) { | 
|  | 6761 | MatchOp  = TI->getOperand(0); | 
|  | 6762 | OtherOpT = TI->getOperand(1); | 
|  | 6763 | OtherOpF = FI->getOperand(1); | 
|  | 6764 | MatchIsOpZero = true; | 
|  | 6765 | } else if (TI->getOperand(1) == FI->getOperand(1)) { | 
|  | 6766 | MatchOp  = TI->getOperand(1); | 
|  | 6767 | OtherOpT = TI->getOperand(0); | 
|  | 6768 | OtherOpF = FI->getOperand(0); | 
|  | 6769 | MatchIsOpZero = false; | 
|  | 6770 | } else if (!TI->isCommutative()) { | 
|  | 6771 | return 0; | 
|  | 6772 | } else if (TI->getOperand(0) == FI->getOperand(1)) { | 
|  | 6773 | MatchOp  = TI->getOperand(0); | 
|  | 6774 | OtherOpT = TI->getOperand(1); | 
|  | 6775 | OtherOpF = FI->getOperand(0); | 
|  | 6776 | MatchIsOpZero = true; | 
|  | 6777 | } else if (TI->getOperand(1) == FI->getOperand(0)) { | 
|  | 6778 | MatchOp  = TI->getOperand(1); | 
|  | 6779 | OtherOpT = TI->getOperand(0); | 
|  | 6780 | OtherOpF = FI->getOperand(1); | 
|  | 6781 | MatchIsOpZero = true; | 
|  | 6782 | } else { | 
|  | 6783 | return 0; | 
|  | 6784 | } | 
|  | 6785 |  | 
|  | 6786 | // If we reach here, they do have operations in common. | 
|  | 6787 | SelectInst *NewSI = new SelectInst(SI.getCondition(), OtherOpT, | 
|  | 6788 | OtherOpF, SI.getName()+".v"); | 
|  | 6789 | InsertNewInstBefore(NewSI, SI); | 
|  | 6790 |  | 
|  | 6791 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TI)) { | 
|  | 6792 | if (MatchIsOpZero) | 
|  | 6793 | return BinaryOperator::create(BO->getOpcode(), MatchOp, NewSI); | 
|  | 6794 | else | 
|  | 6795 | return BinaryOperator::create(BO->getOpcode(), NewSI, MatchOp); | 
| Chris Lattner | 411336f | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 6796 | } | 
| Reid Spencer | 2f34b98 | 2007-02-02 14:41:37 +0000 | [diff] [blame] | 6797 | assert(0 && "Shouldn't get here"); | 
|  | 6798 | return 0; | 
| Chris Lattner | 411336f | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 6799 | } | 
|  | 6800 |  | 
| Chris Lattner | b909e8b | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 6801 | Instruction *InstCombiner::visitSelectInst(SelectInst &SI) { | 
| Chris Lattner | 533bc49 | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 6802 | Value *CondVal = SI.getCondition(); | 
|  | 6803 | Value *TrueVal = SI.getTrueValue(); | 
|  | 6804 | Value *FalseVal = SI.getFalseValue(); | 
|  | 6805 |  | 
|  | 6806 | // select true, X, Y  -> X | 
|  | 6807 | // select false, X, Y -> Y | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 6808 | if (ConstantInt *C = dyn_cast<ConstantInt>(CondVal)) | 
| Reid Spencer | cddc9df | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 6809 | return ReplaceInstUsesWith(SI, C->getZExtValue() ? TrueVal : FalseVal); | 
| Chris Lattner | 533bc49 | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 6810 |  | 
|  | 6811 | // select C, X, X -> X | 
|  | 6812 | if (TrueVal == FalseVal) | 
|  | 6813 | return ReplaceInstUsesWith(SI, TrueVal); | 
|  | 6814 |  | 
| Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 6815 | if (isa<UndefValue>(TrueVal))   // select C, undef, X -> X | 
|  | 6816 | return ReplaceInstUsesWith(SI, FalseVal); | 
|  | 6817 | if (isa<UndefValue>(FalseVal))   // select C, X, undef -> X | 
|  | 6818 | return ReplaceInstUsesWith(SI, TrueVal); | 
|  | 6819 | if (isa<UndefValue>(CondVal)) {  // select undef, X, Y -> X or Y | 
|  | 6820 | if (isa<Constant>(TrueVal)) | 
|  | 6821 | return ReplaceInstUsesWith(SI, TrueVal); | 
|  | 6822 | else | 
|  | 6823 | return ReplaceInstUsesWith(SI, FalseVal); | 
|  | 6824 | } | 
|  | 6825 |  | 
| Reid Spencer | 542964f | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 6826 | if (SI.getType() == Type::Int1Ty) { | 
| Reid Spencer | 7a9c62b | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 6827 | if (ConstantInt *C = dyn_cast<ConstantInt>(TrueVal)) { | 
| Reid Spencer | cddc9df | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 6828 | if (C->getZExtValue()) { | 
| Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 6829 | // Change: A = select B, true, C --> A = or B, C | 
| Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 6830 | return BinaryOperator::createOr(CondVal, FalseVal); | 
| Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 6831 | } else { | 
|  | 6832 | // Change: A = select B, false, C --> A = and !B, C | 
|  | 6833 | Value *NotCond = | 
|  | 6834 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, | 
|  | 6835 | "not."+CondVal->getName()), SI); | 
| Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 6836 | return BinaryOperator::createAnd(NotCond, FalseVal); | 
| Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 6837 | } | 
| Reid Spencer | 7a9c62b | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 6838 | } else if (ConstantInt *C = dyn_cast<ConstantInt>(FalseVal)) { | 
| Reid Spencer | cddc9df | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 6839 | if (C->getZExtValue() == false) { | 
| Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 6840 | // Change: A = select B, C, false --> A = and B, C | 
| Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 6841 | return BinaryOperator::createAnd(CondVal, TrueVal); | 
| Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 6842 | } else { | 
|  | 6843 | // Change: A = select B, C, true --> A = or !B, C | 
|  | 6844 | Value *NotCond = | 
|  | 6845 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, | 
|  | 6846 | "not."+CondVal->getName()), SI); | 
| Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 6847 | return BinaryOperator::createOr(NotCond, TrueVal); | 
| Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 6848 | } | 
|  | 6849 | } | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 6850 | } | 
| Chris Lattner | 1c631e8 | 2004-04-08 04:43:23 +0000 | [diff] [blame] | 6851 |  | 
| Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 6852 | // Selecting between two integer constants? | 
|  | 6853 | if (ConstantInt *TrueValC = dyn_cast<ConstantInt>(TrueVal)) | 
|  | 6854 | if (ConstantInt *FalseValC = dyn_cast<ConstantInt>(FalseVal)) { | 
|  | 6855 | // select C, 1, 0 -> cast C to int | 
| Reid Spencer | 959a21d | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 6856 | if (FalseValC->isZero() && TrueValC->getValue() == 1) { | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6857 | return CastInst::create(Instruction::ZExt, CondVal, SI.getType()); | 
| Reid Spencer | 959a21d | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 6858 | } else if (TrueValC->isZero() && FalseValC->getValue() == 1) { | 
| Chris Lattner | 183b336 | 2004-04-09 19:05:30 +0000 | [diff] [blame] | 6859 | // select C, 0, 1 -> cast !C to int | 
|  | 6860 | Value *NotCond = | 
|  | 6861 | InsertNewInstBefore(BinaryOperator::createNot(CondVal, | 
| Chris Lattner | cf7baf3 | 2004-04-09 18:19:44 +0000 | [diff] [blame] | 6862 | "not."+CondVal->getName()), SI); | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6863 | return CastInst::create(Instruction::ZExt, NotCond, SI.getType()); | 
| Chris Lattner | cf7baf3 | 2004-04-09 18:19:44 +0000 | [diff] [blame] | 6864 | } | 
| Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 6865 |  | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6866 | if (ICmpInst *IC = dyn_cast<ICmpInst>(SI.getCondition())) { | 
| Chris Lattner | 380c7e9 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 6867 |  | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6868 | // (x <s 0) ? -1 : 0 -> ashr x, 31 | 
|  | 6869 | // (x >u 2147483647) ? -1 : 0 -> ashr x, 31 | 
| Reid Spencer | 959a21d | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 6870 | if (TrueValC->isAllOnesValue() && FalseValC->isZero()) | 
| Chris Lattner | 380c7e9 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 6871 | if (ConstantInt *CmpCst = dyn_cast<ConstantInt>(IC->getOperand(1))) { | 
|  | 6872 | bool CanXForm = false; | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6873 | if (IC->isSignedPredicate()) | 
| Reid Spencer | 959a21d | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 6874 | CanXForm = CmpCst->isZero() && | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6875 | IC->getPredicate() == ICmpInst::ICMP_SLT; | 
| Chris Lattner | 380c7e9 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 6876 | else { | 
| Zhou Sheng | 56cda95 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 6877 | uint32_t Bits = CmpCst->getType()->getPrimitiveSizeInBits(); | 
| Reid Spencer | 959a21d | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 6878 | CanXForm = CmpCst->getValue() == APInt::getSignedMaxValue(Bits) && | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6879 | IC->getPredicate() == ICmpInst::ICMP_UGT; | 
| Chris Lattner | 380c7e9 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 6880 | } | 
|  | 6881 |  | 
|  | 6882 | if (CanXForm) { | 
|  | 6883 | // The comparison constant and the result are not neccessarily the | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6884 | // same width. Make an all-ones value by inserting a AShr. | 
| Chris Lattner | 380c7e9 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 6885 | Value *X = IC->getOperand(0); | 
| Zhou Sheng | 56cda95 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 6886 | uint32_t Bits = X->getType()->getPrimitiveSizeInBits(); | 
| Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 6887 | Constant *ShAmt = ConstantInt::get(X->getType(), Bits-1); | 
|  | 6888 | Instruction *SRA = BinaryOperator::create(Instruction::AShr, X, | 
|  | 6889 | ShAmt, "ones"); | 
| Chris Lattner | 380c7e9 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 6890 | InsertNewInstBefore(SRA, SI); | 
|  | 6891 |  | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6892 | // Finally, convert to the type of the select RHS.  We figure out | 
|  | 6893 | // if this requires a SExt, Trunc or BitCast based on the sizes. | 
|  | 6894 | Instruction::CastOps opc = Instruction::BitCast; | 
| Zhou Sheng | 56cda95 | 2007-04-02 08:20:41 +0000 | [diff] [blame] | 6895 | uint32_t SRASize = SRA->getType()->getPrimitiveSizeInBits(); | 
|  | 6896 | uint32_t SISize  = SI.getType()->getPrimitiveSizeInBits(); | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 6897 | if (SRASize < SISize) | 
|  | 6898 | opc = Instruction::SExt; | 
|  | 6899 | else if (SRASize > SISize) | 
|  | 6900 | opc = Instruction::Trunc; | 
|  | 6901 | return CastInst::create(opc, SRA, SI.getType()); | 
| Chris Lattner | 380c7e9 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 6902 | } | 
|  | 6903 | } | 
|  | 6904 |  | 
|  | 6905 |  | 
|  | 6906 | // If one of the constants is zero (we know they can't both be) and we | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6907 | // have a fcmp instruction with zero, and we have an 'and' with the | 
| Chris Lattner | 380c7e9 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 6908 | // non-constant value, eliminate this whole mess.  This corresponds to | 
|  | 6909 | // cases like this: ((X & 27) ? 27 : 0) | 
| Reid Spencer | 959a21d | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 6910 | if (TrueValC->isZero() || FalseValC->isZero()) | 
| Chris Lattner | b3f24c9 | 2006-09-18 04:22:48 +0000 | [diff] [blame] | 6911 | if (IC->isEquality() && isa<ConstantInt>(IC->getOperand(1)) && | 
| Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 6912 | cast<Constant>(IC->getOperand(1))->isNullValue()) | 
|  | 6913 | if (Instruction *ICA = dyn_cast<Instruction>(IC->getOperand(0))) | 
|  | 6914 | if (ICA->getOpcode() == Instruction::And && | 
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 6915 | isa<ConstantInt>(ICA->getOperand(1)) && | 
|  | 6916 | (ICA->getOperand(1) == TrueValC || | 
|  | 6917 | ICA->getOperand(1) == FalseValC) && | 
| Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 6918 | isOneBitSet(cast<ConstantInt>(ICA->getOperand(1)))) { | 
|  | 6919 | // Okay, now we know that everything is set up, we just don't | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6920 | // know whether we have a icmp_ne or icmp_eq and whether the | 
|  | 6921 | // true or false val is the zero. | 
| Reid Spencer | 959a21d | 2007-03-23 21:24:59 +0000 | [diff] [blame] | 6922 | bool ShouldNotVal = !TrueValC->isZero(); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6923 | ShouldNotVal ^= IC->getPredicate() == ICmpInst::ICMP_NE; | 
| Chris Lattner | 35167c3 | 2004-06-09 07:59:58 +0000 | [diff] [blame] | 6924 | Value *V = ICA; | 
|  | 6925 | if (ShouldNotVal) | 
|  | 6926 | V = InsertNewInstBefore(BinaryOperator::create( | 
|  | 6927 | Instruction::Xor, V, ICA->getOperand(1)), SI); | 
|  | 6928 | return ReplaceInstUsesWith(SI, V); | 
|  | 6929 | } | 
| Chris Lattner | 380c7e9 | 2006-09-20 04:44:59 +0000 | [diff] [blame] | 6930 | } | 
| Chris Lattner | 533bc49 | 2004-03-30 19:37:13 +0000 | [diff] [blame] | 6931 | } | 
| Chris Lattner | 623fba1 | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 6932 |  | 
|  | 6933 | // See if we are selecting two values based on a comparison of the two values. | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6934 | if (FCmpInst *FCI = dyn_cast<FCmpInst>(CondVal)) { | 
|  | 6935 | if (FCI->getOperand(0) == TrueVal && FCI->getOperand(1) == FalseVal) { | 
| Chris Lattner | 623fba1 | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 6936 | // Transform (X == Y) ? X : Y  -> Y | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6937 | if (FCI->getPredicate() == FCmpInst::FCMP_OEQ) | 
| Chris Lattner | 623fba1 | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 6938 | return ReplaceInstUsesWith(SI, FalseVal); | 
|  | 6939 | // Transform (X != Y) ? X : Y  -> X | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6940 | if (FCI->getPredicate() == FCmpInst::FCMP_ONE) | 
| Chris Lattner | 623fba1 | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 6941 | return ReplaceInstUsesWith(SI, TrueVal); | 
|  | 6942 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. | 
|  | 6943 |  | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6944 | } else if (FCI->getOperand(0) == FalseVal && FCI->getOperand(1) == TrueVal){ | 
| Chris Lattner | 623fba1 | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 6945 | // Transform (X == Y) ? Y : X  -> X | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6946 | if (FCI->getPredicate() == FCmpInst::FCMP_OEQ) | 
| Chris Lattner | 24cf020 | 2004-04-11 01:39:19 +0000 | [diff] [blame] | 6947 | return ReplaceInstUsesWith(SI, FalseVal); | 
| Chris Lattner | 623fba1 | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 6948 | // Transform (X != Y) ? Y : X  -> Y | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 6949 | if (FCI->getPredicate() == FCmpInst::FCMP_ONE) | 
|  | 6950 | return ReplaceInstUsesWith(SI, TrueVal); | 
|  | 6951 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. | 
|  | 6952 | } | 
|  | 6953 | } | 
|  | 6954 |  | 
|  | 6955 | // See if we are selecting two values based on a comparison of the two values. | 
|  | 6956 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(CondVal)) { | 
|  | 6957 | if (ICI->getOperand(0) == TrueVal && ICI->getOperand(1) == FalseVal) { | 
|  | 6958 | // Transform (X == Y) ? X : Y  -> Y | 
|  | 6959 | if (ICI->getPredicate() == ICmpInst::ICMP_EQ) | 
|  | 6960 | return ReplaceInstUsesWith(SI, FalseVal); | 
|  | 6961 | // Transform (X != Y) ? X : Y  -> X | 
|  | 6962 | if (ICI->getPredicate() == ICmpInst::ICMP_NE) | 
|  | 6963 | return ReplaceInstUsesWith(SI, TrueVal); | 
|  | 6964 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. | 
|  | 6965 |  | 
|  | 6966 | } else if (ICI->getOperand(0) == FalseVal && ICI->getOperand(1) == TrueVal){ | 
|  | 6967 | // Transform (X == Y) ? Y : X  -> X | 
|  | 6968 | if (ICI->getPredicate() == ICmpInst::ICMP_EQ) | 
|  | 6969 | return ReplaceInstUsesWith(SI, FalseVal); | 
|  | 6970 | // Transform (X != Y) ? Y : X  -> Y | 
|  | 6971 | if (ICI->getPredicate() == ICmpInst::ICMP_NE) | 
| Chris Lattner | 24cf020 | 2004-04-11 01:39:19 +0000 | [diff] [blame] | 6972 | return ReplaceInstUsesWith(SI, TrueVal); | 
| Chris Lattner | 623fba1 | 2004-04-10 22:21:27 +0000 | [diff] [blame] | 6973 | // NOTE: if we wanted to, this is where to detect MIN/MAX/ABS/etc. | 
|  | 6974 | } | 
|  | 6975 | } | 
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 6976 |  | 
| Chris Lattner | a04c904 | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 6977 | if (Instruction *TI = dyn_cast<Instruction>(TrueVal)) | 
|  | 6978 | if (Instruction *FI = dyn_cast<Instruction>(FalseVal)) | 
|  | 6979 | if (TI->hasOneUse() && FI->hasOneUse()) { | 
| Chris Lattner | a04c904 | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 6980 | Instruction *AddOp = 0, *SubOp = 0; | 
|  | 6981 |  | 
| Chris Lattner | 411336f | 2005-01-19 21:50:18 +0000 | [diff] [blame] | 6982 | // Turn (select C, (op X, Y), (op X, Z)) -> (op X, (select C, Y, Z)) | 
|  | 6983 | if (TI->getOpcode() == FI->getOpcode()) | 
|  | 6984 | if (Instruction *IV = FoldSelectOpOp(SI, TI, FI)) | 
|  | 6985 | return IV; | 
|  | 6986 |  | 
|  | 6987 | // Turn select C, (X+Y), (X-Y) --> (X+(select C, Y, (-Y))).  This is | 
|  | 6988 | // even legal for FP. | 
| Chris Lattner | a04c904 | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 6989 | if (TI->getOpcode() == Instruction::Sub && | 
|  | 6990 | FI->getOpcode() == Instruction::Add) { | 
|  | 6991 | AddOp = FI; SubOp = TI; | 
|  | 6992 | } else if (FI->getOpcode() == Instruction::Sub && | 
|  | 6993 | TI->getOpcode() == Instruction::Add) { | 
|  | 6994 | AddOp = TI; SubOp = FI; | 
|  | 6995 | } | 
|  | 6996 |  | 
|  | 6997 | if (AddOp) { | 
|  | 6998 | Value *OtherAddOp = 0; | 
|  | 6999 | if (SubOp->getOperand(0) == AddOp->getOperand(0)) { | 
|  | 7000 | OtherAddOp = AddOp->getOperand(1); | 
|  | 7001 | } else if (SubOp->getOperand(0) == AddOp->getOperand(1)) { | 
|  | 7002 | OtherAddOp = AddOp->getOperand(0); | 
|  | 7003 | } | 
|  | 7004 |  | 
|  | 7005 | if (OtherAddOp) { | 
| Chris Lattner | b580d26 | 2006-02-24 18:05:58 +0000 | [diff] [blame] | 7006 | // So at this point we know we have (Y -> OtherAddOp): | 
|  | 7007 | //        select C, (add X, Y), (sub X, Z) | 
|  | 7008 | Value *NegVal;  // Compute -Z | 
|  | 7009 | if (Constant *C = dyn_cast<Constant>(SubOp->getOperand(1))) { | 
|  | 7010 | NegVal = ConstantExpr::getNeg(C); | 
|  | 7011 | } else { | 
|  | 7012 | NegVal = InsertNewInstBefore( | 
|  | 7013 | BinaryOperator::createNeg(SubOp->getOperand(1), "tmp"), SI); | 
| Chris Lattner | a04c904 | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 7014 | } | 
| Chris Lattner | b580d26 | 2006-02-24 18:05:58 +0000 | [diff] [blame] | 7015 |  | 
|  | 7016 | Value *NewTrueOp = OtherAddOp; | 
|  | 7017 | Value *NewFalseOp = NegVal; | 
|  | 7018 | if (AddOp != TI) | 
|  | 7019 | std::swap(NewTrueOp, NewFalseOp); | 
|  | 7020 | Instruction *NewSel = | 
|  | 7021 | new SelectInst(CondVal, NewTrueOp,NewFalseOp,SI.getName()+".p"); | 
|  | 7022 |  | 
|  | 7023 | NewSel = InsertNewInstBefore(NewSel, SI); | 
|  | 7024 | return BinaryOperator::createAdd(SubOp->getOperand(0), NewSel); | 
| Chris Lattner | a04c904 | 2005-01-13 22:52:24 +0000 | [diff] [blame] | 7025 | } | 
|  | 7026 | } | 
|  | 7027 | } | 
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7028 |  | 
| Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7029 | // See if we can fold the select into one of our operands. | 
| Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 7030 | if (SI.getType()->isInteger()) { | 
| Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7031 | // See the comment above GetSelectFoldableOperands for a description of the | 
|  | 7032 | // transformation we are doing here. | 
|  | 7033 | if (Instruction *TVI = dyn_cast<Instruction>(TrueVal)) | 
|  | 7034 | if (TVI->hasOneUse() && TVI->getNumOperands() == 2 && | 
|  | 7035 | !isa<Constant>(FalseVal)) | 
|  | 7036 | if (unsigned SFO = GetSelectFoldableOperands(TVI)) { | 
|  | 7037 | unsigned OpToFold = 0; | 
|  | 7038 | if ((SFO & 1) && FalseVal == TVI->getOperand(0)) { | 
|  | 7039 | OpToFold = 1; | 
|  | 7040 | } else  if ((SFO & 2) && FalseVal == TVI->getOperand(1)) { | 
|  | 7041 | OpToFold = 2; | 
|  | 7042 | } | 
|  | 7043 |  | 
|  | 7044 | if (OpToFold) { | 
|  | 7045 | Constant *C = GetSelectFoldableConstant(TVI); | 
| Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7046 | Instruction *NewSel = | 
| Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 7047 | new SelectInst(SI.getCondition(), TVI->getOperand(2-OpToFold), C); | 
| Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7048 | InsertNewInstBefore(NewSel, SI); | 
| Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 7049 | NewSel->takeName(TVI); | 
| Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7050 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TVI)) | 
|  | 7051 | return BinaryOperator::create(BO->getOpcode(), FalseVal, NewSel); | 
| Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7052 | else { | 
|  | 7053 | assert(0 && "Unknown instruction!!"); | 
|  | 7054 | } | 
|  | 7055 | } | 
|  | 7056 | } | 
| Chris Lattner | 6862fbd | 2004-09-29 17:40:11 +0000 | [diff] [blame] | 7057 |  | 
| Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7058 | if (Instruction *FVI = dyn_cast<Instruction>(FalseVal)) | 
|  | 7059 | if (FVI->hasOneUse() && FVI->getNumOperands() == 2 && | 
|  | 7060 | !isa<Constant>(TrueVal)) | 
|  | 7061 | if (unsigned SFO = GetSelectFoldableOperands(FVI)) { | 
|  | 7062 | unsigned OpToFold = 0; | 
|  | 7063 | if ((SFO & 1) && TrueVal == FVI->getOperand(0)) { | 
|  | 7064 | OpToFold = 1; | 
|  | 7065 | } else  if ((SFO & 2) && TrueVal == FVI->getOperand(1)) { | 
|  | 7066 | OpToFold = 2; | 
|  | 7067 | } | 
|  | 7068 |  | 
|  | 7069 | if (OpToFold) { | 
|  | 7070 | Constant *C = GetSelectFoldableConstant(FVI); | 
| Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7071 | Instruction *NewSel = | 
| Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 7072 | new SelectInst(SI.getCondition(), C, FVI->getOperand(2-OpToFold)); | 
| Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7073 | InsertNewInstBefore(NewSel, SI); | 
| Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 7074 | NewSel->takeName(FVI); | 
| Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7075 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(FVI)) | 
|  | 7076 | return BinaryOperator::create(BO->getOpcode(), TrueVal, NewSel); | 
| Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7077 | else | 
| Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7078 | assert(0 && "Unknown instruction!!"); | 
| Chris Lattner | 56e4d3d | 2004-04-09 23:46:01 +0000 | [diff] [blame] | 7079 | } | 
|  | 7080 | } | 
|  | 7081 | } | 
| Chris Lattner | d6f636a | 2005-04-24 07:30:14 +0000 | [diff] [blame] | 7082 |  | 
|  | 7083 | if (BinaryOperator::isNot(CondVal)) { | 
|  | 7084 | SI.setOperand(0, BinaryOperator::getNotArgument(CondVal)); | 
|  | 7085 | SI.setOperand(1, FalseVal); | 
|  | 7086 | SI.setOperand(2, TrueVal); | 
|  | 7087 | return &SI; | 
|  | 7088 | } | 
|  | 7089 |  | 
| Chris Lattner | b909e8b | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 7090 | return 0; | 
|  | 7091 | } | 
|  | 7092 |  | 
| Chris Lattner | 82f2ef2 | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7093 | /// GetKnownAlignment - If the specified pointer has an alignment that we can | 
|  | 7094 | /// determine, return it, otherwise return 0. | 
|  | 7095 | static unsigned GetKnownAlignment(Value *V, TargetData *TD) { | 
|  | 7096 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) { | 
|  | 7097 | unsigned Align = GV->getAlignment(); | 
|  | 7098 | if (Align == 0 && TD) | 
| Chris Lattner | 945e437 | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 7099 | Align = TD->getPrefTypeAlignment(GV->getType()->getElementType()); | 
| Chris Lattner | 82f2ef2 | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7100 | return Align; | 
|  | 7101 | } else if (AllocationInst *AI = dyn_cast<AllocationInst>(V)) { | 
|  | 7102 | unsigned Align = AI->getAlignment(); | 
|  | 7103 | if (Align == 0 && TD) { | 
|  | 7104 | if (isa<AllocaInst>(AI)) | 
| Chris Lattner | 945e437 | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 7105 | Align = TD->getPrefTypeAlignment(AI->getType()->getElementType()); | 
| Chris Lattner | 82f2ef2 | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7106 | else if (isa<MallocInst>(AI)) { | 
|  | 7107 | // Malloc returns maximally aligned memory. | 
| Chris Lattner | 945e437 | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 7108 | Align = TD->getABITypeAlignment(AI->getType()->getElementType()); | 
| Chris Lattner | 50ee0e4 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 7109 | Align = | 
|  | 7110 | std::max(Align, | 
| Chris Lattner | 945e437 | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 7111 | (unsigned)TD->getABITypeAlignment(Type::DoubleTy)); | 
| Chris Lattner | 50ee0e4 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 7112 | Align = | 
|  | 7113 | std::max(Align, | 
| Chris Lattner | 945e437 | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 7114 | (unsigned)TD->getABITypeAlignment(Type::Int64Ty)); | 
| Chris Lattner | 82f2ef2 | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7115 | } | 
|  | 7116 | } | 
|  | 7117 | return Align; | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7118 | } else if (isa<BitCastInst>(V) || | 
| Chris Lattner | 53ef5a0 | 2006-03-07 01:28:57 +0000 | [diff] [blame] | 7119 | (isa<ConstantExpr>(V) && | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7120 | cast<ConstantExpr>(V)->getOpcode() == Instruction::BitCast)) { | 
| Chris Lattner | 53ef5a0 | 2006-03-07 01:28:57 +0000 | [diff] [blame] | 7121 | User *CI = cast<User>(V); | 
| Chris Lattner | 82f2ef2 | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7122 | if (isa<PointerType>(CI->getOperand(0)->getType())) | 
|  | 7123 | return GetKnownAlignment(CI->getOperand(0), TD); | 
|  | 7124 | return 0; | 
| Chris Lattner | 53ef5a0 | 2006-03-07 01:28:57 +0000 | [diff] [blame] | 7125 | } else if (isa<GetElementPtrInst>(V) || | 
|  | 7126 | (isa<ConstantExpr>(V) && | 
|  | 7127 | cast<ConstantExpr>(V)->getOpcode()==Instruction::GetElementPtr)) { | 
|  | 7128 | User *GEPI = cast<User>(V); | 
| Chris Lattner | 82f2ef2 | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7129 | unsigned BaseAlignment = GetKnownAlignment(GEPI->getOperand(0), TD); | 
|  | 7130 | if (BaseAlignment == 0) return 0; | 
|  | 7131 |  | 
|  | 7132 | // If all indexes are zero, it is just the alignment of the base pointer. | 
|  | 7133 | bool AllZeroOperands = true; | 
|  | 7134 | for (unsigned i = 1, e = GEPI->getNumOperands(); i != e; ++i) | 
|  | 7135 | if (!isa<Constant>(GEPI->getOperand(i)) || | 
|  | 7136 | !cast<Constant>(GEPI->getOperand(i))->isNullValue()) { | 
|  | 7137 | AllZeroOperands = false; | 
|  | 7138 | break; | 
|  | 7139 | } | 
|  | 7140 | if (AllZeroOperands) | 
|  | 7141 | return BaseAlignment; | 
|  | 7142 |  | 
|  | 7143 | // Otherwise, if the base alignment is >= the alignment we expect for the | 
|  | 7144 | // base pointer type, then we know that the resultant pointer is aligned at | 
|  | 7145 | // least as much as its type requires. | 
|  | 7146 | if (!TD) return 0; | 
|  | 7147 |  | 
|  | 7148 | const Type *BasePtrTy = GEPI->getOperand(0)->getType(); | 
| Chris Lattner | 50ee0e4 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 7149 | const PointerType *PtrTy = cast<PointerType>(BasePtrTy); | 
| Chris Lattner | 945e437 | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 7150 | if (TD->getABITypeAlignment(PtrTy->getElementType()) | 
| Chris Lattner | 53ef5a0 | 2006-03-07 01:28:57 +0000 | [diff] [blame] | 7151 | <= BaseAlignment) { | 
|  | 7152 | const Type *GEPTy = GEPI->getType(); | 
| Chris Lattner | 50ee0e4 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 7153 | const PointerType *GEPPtrTy = cast<PointerType>(GEPTy); | 
| Chris Lattner | 945e437 | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 7154 | return TD->getABITypeAlignment(GEPPtrTy->getElementType()); | 
| Chris Lattner | 53ef5a0 | 2006-03-07 01:28:57 +0000 | [diff] [blame] | 7155 | } | 
| Chris Lattner | 82f2ef2 | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7156 | return 0; | 
|  | 7157 | } | 
|  | 7158 | return 0; | 
|  | 7159 | } | 
|  | 7160 |  | 
| Chris Lattner | b909e8b | 2004-03-12 05:52:32 +0000 | [diff] [blame] | 7161 |  | 
| Chris Lattner | c66b223 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 7162 | /// visitCallInst - CallInst simplification.  This mostly only handles folding | 
|  | 7163 | /// of intrinsic instructions.  For normal calls, it allows visitCallSite to do | 
|  | 7164 | /// the heavy lifting. | 
|  | 7165 | /// | 
| Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7166 | Instruction *InstCombiner::visitCallInst(CallInst &CI) { | 
| Chris Lattner | c66b223 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 7167 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(&CI); | 
|  | 7168 | if (!II) return visitCallSite(&CI); | 
|  | 7169 |  | 
| Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 7170 | // Intrinsics cannot occur in an invoke, so handle them here instead of in | 
|  | 7171 | // visitCallSite. | 
| Chris Lattner | c66b223 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 7172 | if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(II)) { | 
| Chris Lattner | 00648e1 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 7173 | bool Changed = false; | 
|  | 7174 |  | 
|  | 7175 | // memmove/cpy/set of zero bytes is a noop. | 
|  | 7176 | if (Constant *NumBytes = dyn_cast<Constant>(MI->getLength())) { | 
|  | 7177 | if (NumBytes->isNullValue()) return EraseInstFromFunction(CI); | 
|  | 7178 |  | 
| Chris Lattner | 00648e1 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 7179 | if (ConstantInt *CI = dyn_cast<ConstantInt>(NumBytes)) | 
| Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 7180 | if (CI->getZExtValue() == 1) { | 
| Chris Lattner | 00648e1 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 7181 | // Replace the instruction with just byte operations.  We would | 
|  | 7182 | // transform other cases to loads/stores, but we don't know if | 
|  | 7183 | // alignment is sufficient. | 
|  | 7184 | } | 
| Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 7185 | } | 
|  | 7186 |  | 
| Chris Lattner | 00648e1 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 7187 | // If we have a memmove and the source operation is a constant global, | 
|  | 7188 | // then the source and dest pointers can't alias, so we can change this | 
|  | 7189 | // into a call to memcpy. | 
| Chris Lattner | 82f2ef2 | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7190 | if (MemMoveInst *MMI = dyn_cast<MemMoveInst>(II)) { | 
| Chris Lattner | 00648e1 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 7191 | if (GlobalVariable *GVSrc = dyn_cast<GlobalVariable>(MMI->getSource())) | 
|  | 7192 | if (GVSrc->isConstant()) { | 
|  | 7193 | Module *M = CI.getParent()->getParent()->getParent(); | 
| Chris Lattner | 681ef2f | 2006-03-03 01:34:17 +0000 | [diff] [blame] | 7194 | const char *Name; | 
| Andrew Lenharth | 0ebb0b0 | 2006-11-03 22:45:50 +0000 | [diff] [blame] | 7195 | if (CI.getCalledFunction()->getFunctionType()->getParamType(2) == | 
| Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 7196 | Type::Int32Ty) | 
| Chris Lattner | 681ef2f | 2006-03-03 01:34:17 +0000 | [diff] [blame] | 7197 | Name = "llvm.memcpy.i32"; | 
|  | 7198 | else | 
|  | 7199 | Name = "llvm.memcpy.i64"; | 
| Chris Lattner | fbc524f | 2007-01-07 06:58:05 +0000 | [diff] [blame] | 7200 | Constant *MemCpy = M->getOrInsertFunction(Name, | 
| Chris Lattner | 00648e1 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 7201 | CI.getCalledFunction()->getFunctionType()); | 
|  | 7202 | CI.setOperand(0, MemCpy); | 
|  | 7203 | Changed = true; | 
|  | 7204 | } | 
| Chris Lattner | 82f2ef2 | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7205 | } | 
| Chris Lattner | 00648e1 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 7206 |  | 
| Chris Lattner | 82f2ef2 | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7207 | // If we can determine a pointer alignment that is bigger than currently | 
|  | 7208 | // set, update the alignment. | 
|  | 7209 | if (isa<MemCpyInst>(MI) || isa<MemMoveInst>(MI)) { | 
|  | 7210 | unsigned Alignment1 = GetKnownAlignment(MI->getOperand(1), TD); | 
|  | 7211 | unsigned Alignment2 = GetKnownAlignment(MI->getOperand(2), TD); | 
|  | 7212 | unsigned Align = std::min(Alignment1, Alignment2); | 
| Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 7213 | if (MI->getAlignment()->getZExtValue() < Align) { | 
| Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 7214 | MI->setAlignment(ConstantInt::get(Type::Int32Ty, Align)); | 
| Chris Lattner | 82f2ef2 | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7215 | Changed = true; | 
|  | 7216 | } | 
|  | 7217 | } else if (isa<MemSetInst>(MI)) { | 
|  | 7218 | unsigned Alignment = GetKnownAlignment(MI->getDest(), TD); | 
| Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 7219 | if (MI->getAlignment()->getZExtValue() < Alignment) { | 
| Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 7220 | MI->setAlignment(ConstantInt::get(Type::Int32Ty, Alignment)); | 
| Chris Lattner | 82f2ef2 | 2006-03-06 20:18:44 +0000 | [diff] [blame] | 7221 | Changed = true; | 
|  | 7222 | } | 
|  | 7223 | } | 
|  | 7224 |  | 
| Chris Lattner | c66b223 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 7225 | if (Changed) return II; | 
| Chris Lattner | 503221f | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 7226 | } else { | 
|  | 7227 | switch (II->getIntrinsicID()) { | 
|  | 7228 | default: break; | 
| Chris Lattner | f42d0ae | 2006-04-02 05:30:25 +0000 | [diff] [blame] | 7229 | case Intrinsic::ppc_altivec_lvx: | 
|  | 7230 | case Intrinsic::ppc_altivec_lvxl: | 
| Chris Lattner | 36dd7c9 | 2006-04-17 22:26:56 +0000 | [diff] [blame] | 7231 | case Intrinsic::x86_sse_loadu_ps: | 
|  | 7232 | case Intrinsic::x86_sse2_loadu_pd: | 
|  | 7233 | case Intrinsic::x86_sse2_loadu_dq: | 
|  | 7234 | // Turn PPC lvx     -> load if the pointer is known aligned. | 
|  | 7235 | // Turn X86 loadups -> load if the pointer is known aligned. | 
| Chris Lattner | f42d0ae | 2006-04-02 05:30:25 +0000 | [diff] [blame] | 7236 | if (GetKnownAlignment(II->getOperand(1), TD) >= 16) { | 
| Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7237 | Value *Ptr = InsertCastBefore(Instruction::BitCast, II->getOperand(1), | 
| Chris Lattner | e79d249 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 7238 | PointerType::get(II->getType()), CI); | 
| Chris Lattner | f42d0ae | 2006-04-02 05:30:25 +0000 | [diff] [blame] | 7239 | return new LoadInst(Ptr); | 
|  | 7240 | } | 
|  | 7241 | break; | 
|  | 7242 | case Intrinsic::ppc_altivec_stvx: | 
|  | 7243 | case Intrinsic::ppc_altivec_stvxl: | 
|  | 7244 | // Turn stvx -> store if the pointer is known aligned. | 
|  | 7245 | if (GetKnownAlignment(II->getOperand(2), TD) >= 16) { | 
| Chris Lattner | e79d249 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 7246 | const Type *OpPtrTy = PointerType::get(II->getOperand(1)->getType()); | 
| Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7247 | Value *Ptr = InsertCastBefore(Instruction::BitCast, II->getOperand(2), | 
|  | 7248 | OpPtrTy, CI); | 
| Chris Lattner | f42d0ae | 2006-04-02 05:30:25 +0000 | [diff] [blame] | 7249 | return new StoreInst(II->getOperand(1), Ptr); | 
|  | 7250 | } | 
|  | 7251 | break; | 
| Chris Lattner | 36dd7c9 | 2006-04-17 22:26:56 +0000 | [diff] [blame] | 7252 | case Intrinsic::x86_sse_storeu_ps: | 
|  | 7253 | case Intrinsic::x86_sse2_storeu_pd: | 
|  | 7254 | case Intrinsic::x86_sse2_storeu_dq: | 
|  | 7255 | case Intrinsic::x86_sse2_storel_dq: | 
|  | 7256 | // Turn X86 storeu -> store if the pointer is known aligned. | 
|  | 7257 | if (GetKnownAlignment(II->getOperand(1), TD) >= 16) { | 
|  | 7258 | const Type *OpPtrTy = PointerType::get(II->getOperand(2)->getType()); | 
| Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7259 | Value *Ptr = InsertCastBefore(Instruction::BitCast, II->getOperand(1), | 
|  | 7260 | OpPtrTy, CI); | 
| Chris Lattner | 36dd7c9 | 2006-04-17 22:26:56 +0000 | [diff] [blame] | 7261 | return new StoreInst(II->getOperand(2), Ptr); | 
|  | 7262 | } | 
|  | 7263 | break; | 
| Chris Lattner | 2deeaea | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 7264 |  | 
|  | 7265 | case Intrinsic::x86_sse_cvttss2si: { | 
|  | 7266 | // These intrinsics only demands the 0th element of its input vector.  If | 
|  | 7267 | // we can simplify the input based on that, do so now. | 
|  | 7268 | uint64_t UndefElts; | 
|  | 7269 | if (Value *V = SimplifyDemandedVectorElts(II->getOperand(1), 1, | 
|  | 7270 | UndefElts)) { | 
|  | 7271 | II->setOperand(1, V); | 
|  | 7272 | return II; | 
|  | 7273 | } | 
|  | 7274 | break; | 
|  | 7275 | } | 
|  | 7276 |  | 
| Chris Lattner | e79d249 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 7277 | case Intrinsic::ppc_altivec_vperm: | 
|  | 7278 | // Turn vperm(V1,V2,mask) -> shuffle(V1,V2,mask) if mask is a constant. | 
| Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 7279 | if (ConstantVector *Mask = dyn_cast<ConstantVector>(II->getOperand(3))) { | 
| Chris Lattner | e79d249 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 7280 | assert(Mask->getNumOperands() == 16 && "Bad type for intrinsic!"); | 
|  | 7281 |  | 
|  | 7282 | // Check that all of the elements are integer constants or undefs. | 
|  | 7283 | bool AllEltsOk = true; | 
|  | 7284 | for (unsigned i = 0; i != 16; ++i) { | 
|  | 7285 | if (!isa<ConstantInt>(Mask->getOperand(i)) && | 
|  | 7286 | !isa<UndefValue>(Mask->getOperand(i))) { | 
|  | 7287 | AllEltsOk = false; | 
|  | 7288 | break; | 
|  | 7289 | } | 
|  | 7290 | } | 
|  | 7291 |  | 
|  | 7292 | if (AllEltsOk) { | 
|  | 7293 | // Cast the input vectors to byte vectors. | 
| Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7294 | Value *Op0 = InsertCastBefore(Instruction::BitCast, | 
|  | 7295 | II->getOperand(1), Mask->getType(), CI); | 
|  | 7296 | Value *Op1 = InsertCastBefore(Instruction::BitCast, | 
|  | 7297 | II->getOperand(2), Mask->getType(), CI); | 
| Chris Lattner | e79d249 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 7298 | Value *Result = UndefValue::get(Op0->getType()); | 
|  | 7299 |  | 
|  | 7300 | // Only extract each element once. | 
|  | 7301 | Value *ExtractedElts[32]; | 
|  | 7302 | memset(ExtractedElts, 0, sizeof(ExtractedElts)); | 
|  | 7303 |  | 
|  | 7304 | for (unsigned i = 0; i != 16; ++i) { | 
|  | 7305 | if (isa<UndefValue>(Mask->getOperand(i))) | 
|  | 7306 | continue; | 
| Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 7307 | unsigned Idx =cast<ConstantInt>(Mask->getOperand(i))->getZExtValue(); | 
| Chris Lattner | e79d249 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 7308 | Idx &= 31;  // Match the hardware behavior. | 
|  | 7309 |  | 
|  | 7310 | if (ExtractedElts[Idx] == 0) { | 
|  | 7311 | Instruction *Elt = | 
| Chris Lattner | 2deeaea | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 7312 | new ExtractElementInst(Idx < 16 ? Op0 : Op1, Idx&15, "tmp"); | 
| Chris Lattner | e79d249 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 7313 | InsertNewInstBefore(Elt, CI); | 
|  | 7314 | ExtractedElts[Idx] = Elt; | 
|  | 7315 | } | 
|  | 7316 |  | 
|  | 7317 | // Insert this value into the result vector. | 
| Chris Lattner | 2deeaea | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 7318 | Result = new InsertElementInst(Result, ExtractedElts[Idx], i,"tmp"); | 
| Chris Lattner | e79d249 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 7319 | InsertNewInstBefore(cast<Instruction>(Result), CI); | 
|  | 7320 | } | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7321 | return CastInst::create(Instruction::BitCast, Result, CI.getType()); | 
| Chris Lattner | e79d249 | 2006-04-06 19:19:17 +0000 | [diff] [blame] | 7322 | } | 
|  | 7323 | } | 
|  | 7324 | break; | 
|  | 7325 |  | 
| Chris Lattner | 503221f | 2006-01-13 21:28:09 +0000 | [diff] [blame] | 7326 | case Intrinsic::stackrestore: { | 
|  | 7327 | // If the save is right next to the restore, remove the restore.  This can | 
|  | 7328 | // happen when variable allocas are DCE'd. | 
|  | 7329 | if (IntrinsicInst *SS = dyn_cast<IntrinsicInst>(II->getOperand(1))) { | 
|  | 7330 | if (SS->getIntrinsicID() == Intrinsic::stacksave) { | 
|  | 7331 | BasicBlock::iterator BI = SS; | 
|  | 7332 | if (&*++BI == II) | 
|  | 7333 | return EraseInstFromFunction(CI); | 
|  | 7334 | } | 
|  | 7335 | } | 
|  | 7336 |  | 
|  | 7337 | // If the stack restore is in a return/unwind block and if there are no | 
|  | 7338 | // allocas or calls between the restore and the return, nuke the restore. | 
|  | 7339 | TerminatorInst *TI = II->getParent()->getTerminator(); | 
|  | 7340 | if (isa<ReturnInst>(TI) || isa<UnwindInst>(TI)) { | 
|  | 7341 | BasicBlock::iterator BI = II; | 
|  | 7342 | bool CannotRemove = false; | 
|  | 7343 | for (++BI; &*BI != TI; ++BI) { | 
|  | 7344 | if (isa<AllocaInst>(BI) || | 
|  | 7345 | (isa<CallInst>(BI) && !isa<IntrinsicInst>(BI))) { | 
|  | 7346 | CannotRemove = true; | 
|  | 7347 | break; | 
|  | 7348 | } | 
|  | 7349 | } | 
|  | 7350 | if (!CannotRemove) | 
|  | 7351 | return EraseInstFromFunction(CI); | 
|  | 7352 | } | 
|  | 7353 | break; | 
|  | 7354 | } | 
|  | 7355 | } | 
| Chris Lattner | 00648e1 | 2004-10-12 04:52:52 +0000 | [diff] [blame] | 7356 | } | 
|  | 7357 |  | 
| Chris Lattner | c66b223 | 2006-01-13 20:11:04 +0000 | [diff] [blame] | 7358 | return visitCallSite(II); | 
| Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7359 | } | 
|  | 7360 |  | 
|  | 7361 | // InvokeInst simplification | 
|  | 7362 | // | 
|  | 7363 | Instruction *InstCombiner::visitInvokeInst(InvokeInst &II) { | 
| Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 7364 | return visitCallSite(&II); | 
| Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7365 | } | 
|  | 7366 |  | 
| Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 7367 | // visitCallSite - Improvements for call and invoke instructions. | 
|  | 7368 | // | 
|  | 7369 | Instruction *InstCombiner::visitCallSite(CallSite CS) { | 
| Chris Lattner | 75b4d1d | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 7370 | bool Changed = false; | 
|  | 7371 |  | 
|  | 7372 | // If the callee is a constexpr cast of a function, attempt to move the cast | 
|  | 7373 | // to the arguments of the call/invoke. | 
| Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 7374 | if (transformConstExprCastCall(CS)) return 0; | 
|  | 7375 |  | 
| Chris Lattner | 75b4d1d | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 7376 | Value *Callee = CS.getCalledValue(); | 
| Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 7377 |  | 
| Chris Lattner | 61d9d81 | 2005-05-13 07:09:09 +0000 | [diff] [blame] | 7378 | if (Function *CalleeF = dyn_cast<Function>(Callee)) | 
|  | 7379 | if (CalleeF->getCallingConv() != CS.getCallingConv()) { | 
|  | 7380 | Instruction *OldCall = CS.getInstruction(); | 
|  | 7381 | // If the call and callee calling conventions don't match, this call must | 
|  | 7382 | // be unreachable, as the call is undefined. | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 7383 | new StoreInst(ConstantInt::getTrue(), | 
| Reid Spencer | 542964f | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 7384 | UndefValue::get(PointerType::get(Type::Int1Ty)), OldCall); | 
| Chris Lattner | 61d9d81 | 2005-05-13 07:09:09 +0000 | [diff] [blame] | 7385 | if (!OldCall->use_empty()) | 
|  | 7386 | OldCall->replaceAllUsesWith(UndefValue::get(OldCall->getType())); | 
|  | 7387 | if (isa<CallInst>(OldCall))   // Not worth removing an invoke here. | 
|  | 7388 | return EraseInstFromFunction(*OldCall); | 
|  | 7389 | return 0; | 
|  | 7390 | } | 
|  | 7391 |  | 
| Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 7392 | if (isa<ConstantPointerNull>(Callee) || isa<UndefValue>(Callee)) { | 
|  | 7393 | // This instruction is not reachable, just remove it.  We insert a store to | 
|  | 7394 | // undef so that we know that this code is not reachable, despite the fact | 
|  | 7395 | // that we can't modify the CFG here. | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 7396 | new StoreInst(ConstantInt::getTrue(), | 
| Reid Spencer | 542964f | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 7397 | UndefValue::get(PointerType::get(Type::Int1Ty)), | 
| Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 7398 | CS.getInstruction()); | 
|  | 7399 |  | 
|  | 7400 | if (!CS.getInstruction()->use_empty()) | 
|  | 7401 | CS.getInstruction()-> | 
|  | 7402 | replaceAllUsesWith(UndefValue::get(CS.getInstruction()->getType())); | 
|  | 7403 |  | 
|  | 7404 | if (InvokeInst *II = dyn_cast<InvokeInst>(CS.getInstruction())) { | 
|  | 7405 | // Don't break the CFG, insert a dummy cond branch. | 
|  | 7406 | new BranchInst(II->getNormalDest(), II->getUnwindDest(), | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 7407 | ConstantInt::getTrue(), II); | 
| Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 7408 | } | 
| Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 7409 | return EraseInstFromFunction(*CS.getInstruction()); | 
|  | 7410 | } | 
| Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 7411 |  | 
| Chris Lattner | 75b4d1d | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 7412 | const PointerType *PTy = cast<PointerType>(Callee->getType()); | 
|  | 7413 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); | 
|  | 7414 | if (FTy->isVarArg()) { | 
|  | 7415 | // See if we can optimize any arguments passed through the varargs area of | 
|  | 7416 | // the call. | 
|  | 7417 | for (CallSite::arg_iterator I = CS.arg_begin()+FTy->getNumParams(), | 
|  | 7418 | E = CS.arg_end(); I != E; ++I) | 
|  | 7419 | if (CastInst *CI = dyn_cast<CastInst>(*I)) { | 
|  | 7420 | // If this cast does not effect the value passed through the varargs | 
|  | 7421 | // area, we can eliminate the use of the cast. | 
|  | 7422 | Value *Op = CI->getOperand(0); | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7423 | if (CI->isLosslessCast()) { | 
| Chris Lattner | 75b4d1d | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 7424 | *I = Op; | 
|  | 7425 | Changed = true; | 
|  | 7426 | } | 
|  | 7427 | } | 
|  | 7428 | } | 
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7429 |  | 
| Chris Lattner | 75b4d1d | 2003-10-07 22:54:13 +0000 | [diff] [blame] | 7430 | return Changed ? CS.getInstruction() : 0; | 
| Chris Lattner | aec3d94 | 2003-10-07 22:32:43 +0000 | [diff] [blame] | 7431 | } | 
|  | 7432 |  | 
| Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7433 | // transformConstExprCastCall - If the callee is a constexpr cast of a function, | 
|  | 7434 | // attempt to move the cast to the arguments of the call/invoke. | 
|  | 7435 | // | 
|  | 7436 | bool InstCombiner::transformConstExprCastCall(CallSite CS) { | 
|  | 7437 | if (!isa<ConstantExpr>(CS.getCalledValue())) return false; | 
|  | 7438 | ConstantExpr *CE = cast<ConstantExpr>(CS.getCalledValue()); | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7439 | if (CE->getOpcode() != Instruction::BitCast || | 
|  | 7440 | !isa<Function>(CE->getOperand(0))) | 
| Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7441 | return false; | 
| Reid Spencer | 8743687 | 2004-07-18 00:38:32 +0000 | [diff] [blame] | 7442 | Function *Callee = cast<Function>(CE->getOperand(0)); | 
| Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7443 | Instruction *Caller = CS.getInstruction(); | 
|  | 7444 |  | 
|  | 7445 | // Okay, this is a cast from a function to a different type.  Unless doing so | 
|  | 7446 | // would cause a type conversion of one of our arguments, change this call to | 
|  | 7447 | // be a direct call with arguments casted to the appropriate types. | 
|  | 7448 | // | 
|  | 7449 | const FunctionType *FT = Callee->getFunctionType(); | 
|  | 7450 | const Type *OldRetTy = Caller->getType(); | 
|  | 7451 |  | 
| Chris Lattner | 1f7942f | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 7452 | // Check to see if we are changing the return type... | 
|  | 7453 | if (OldRetTy != FT->getReturnType()) { | 
| Reid Spencer | 5301e7c | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 7454 | if (Callee->isDeclaration() && !Caller->use_empty() && | 
| Chris Lattner | 7051d75 | 2007-01-06 19:53:32 +0000 | [diff] [blame] | 7455 | // Conversion is ok if changing from pointer to int of same size. | 
|  | 7456 | !(isa<PointerType>(FT->getReturnType()) && | 
|  | 7457 | TD->getIntPtrType() == OldRetTy)) | 
| Chris Lattner | 400f959 | 2007-01-06 02:09:32 +0000 | [diff] [blame] | 7458 | return false;   // Cannot transform this return value. | 
| Chris Lattner | 1f7942f | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 7459 |  | 
|  | 7460 | // If the callsite is an invoke instruction, and the return value is used by | 
|  | 7461 | // a PHI node in a successor, we cannot change the return type of the call | 
|  | 7462 | // because there is no place to put the cast instruction (without breaking | 
|  | 7463 | // the critical edge).  Bail out in this case. | 
|  | 7464 | if (!Caller->use_empty()) | 
|  | 7465 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) | 
|  | 7466 | for (Value::use_iterator UI = II->use_begin(), E = II->use_end(); | 
|  | 7467 | UI != E; ++UI) | 
|  | 7468 | if (PHINode *PN = dyn_cast<PHINode>(*UI)) | 
|  | 7469 | if (PN->getParent() == II->getNormalDest() || | 
| Chris Lattner | fae8ab3 | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 7470 | PN->getParent() == II->getUnwindDest()) | 
| Chris Lattner | 1f7942f | 2004-01-14 06:06:08 +0000 | [diff] [blame] | 7471 | return false; | 
|  | 7472 | } | 
| Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7473 |  | 
|  | 7474 | unsigned NumActualArgs = unsigned(CS.arg_end()-CS.arg_begin()); | 
|  | 7475 | unsigned NumCommonArgs = std::min(FT->getNumParams(), NumActualArgs); | 
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7476 |  | 
| Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7477 | CallSite::arg_iterator AI = CS.arg_begin(); | 
|  | 7478 | for (unsigned i = 0, e = NumCommonArgs; i != e; ++i, ++AI) { | 
|  | 7479 | const Type *ParamTy = FT->getParamType(i); | 
| Andrew Lenharth | ebfa24e | 2006-06-28 01:01:52 +0000 | [diff] [blame] | 7480 | const Type *ActTy = (*AI)->getType(); | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7481 | ConstantInt *c = dyn_cast<ConstantInt>(*AI); | 
| Dale Johannesen | 7c2001d | 2007-04-04 19:16:42 +0000 | [diff] [blame] | 7482 | //Some conversions are safe even if we do not have a body. | 
| Andrew Lenharth | ebfa24e | 2006-06-28 01:01:52 +0000 | [diff] [blame] | 7483 | //Either we can cast directly, or we can upconvert the argument | 
| Chris Lattner | 400f959 | 2007-01-06 02:09:32 +0000 | [diff] [blame] | 7484 | bool isConvertible = ActTy == ParamTy || | 
| Chris Lattner | 7051d75 | 2007-01-06 19:53:32 +0000 | [diff] [blame] | 7485 | (isa<PointerType>(ParamTy) && isa<PointerType>(ActTy)) || | 
| Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 7486 | (ParamTy->isInteger() && ActTy->isInteger() && | 
| Reid Spencer | 8f166b0 | 2007-01-08 16:32:00 +0000 | [diff] [blame] | 7487 | ParamTy->getPrimitiveSizeInBits() >= ActTy->getPrimitiveSizeInBits()) || | 
|  | 7488 | (c && ParamTy->getPrimitiveSizeInBits() >= ActTy->getPrimitiveSizeInBits() | 
| Zhou Sheng | 222d5eb | 2007-03-25 05:01:29 +0000 | [diff] [blame] | 7489 | && c->getValue().isStrictlyPositive()); | 
| Reid Spencer | 5301e7c | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 7490 | if (Callee->isDeclaration() && !isConvertible) return false; | 
| Dale Johannesen | 7c2001d | 2007-04-04 19:16:42 +0000 | [diff] [blame] | 7491 |  | 
|  | 7492 | // Most other conversions can be done if we have a body, even if these | 
|  | 7493 | // lose information, e.g. int->short. | 
|  | 7494 | // Some conversions cannot be done at all, e.g. float to pointer. | 
|  | 7495 | // Logic here parallels CastInst::getCastOpcode (the design there | 
|  | 7496 | // requires legality checks like this be done before calling it). | 
|  | 7497 | if (ParamTy->isInteger()) { | 
|  | 7498 | if (const VectorType *VActTy = dyn_cast<VectorType>(ActTy)) { | 
|  | 7499 | if (VActTy->getBitWidth() != ParamTy->getPrimitiveSizeInBits()) | 
|  | 7500 | return false; | 
|  | 7501 | } | 
|  | 7502 | if (!ActTy->isInteger() && !ActTy->isFloatingPoint() && | 
|  | 7503 | !isa<PointerType>(ActTy)) | 
|  | 7504 | return false; | 
|  | 7505 | } else if (ParamTy->isFloatingPoint()) { | 
|  | 7506 | if (const VectorType *VActTy = dyn_cast<VectorType>(ActTy)) { | 
|  | 7507 | if (VActTy->getBitWidth() != ParamTy->getPrimitiveSizeInBits()) | 
|  | 7508 | return false; | 
|  | 7509 | } | 
|  | 7510 | if (!ActTy->isInteger() && !ActTy->isFloatingPoint()) | 
|  | 7511 | return false; | 
|  | 7512 | } else if (const VectorType *VParamTy = dyn_cast<VectorType>(ParamTy)) { | 
|  | 7513 | if (const VectorType *VActTy = dyn_cast<VectorType>(ActTy)) { | 
|  | 7514 | if (VActTy->getBitWidth() != VParamTy->getBitWidth()) | 
|  | 7515 | return false; | 
|  | 7516 | } | 
|  | 7517 | if (VParamTy->getBitWidth() != ActTy->getPrimitiveSizeInBits()) | 
|  | 7518 | return false; | 
|  | 7519 | } else if (isa<PointerType>(ParamTy)) { | 
|  | 7520 | if (!ActTy->isInteger() && !isa<PointerType>(ActTy)) | 
|  | 7521 | return false; | 
|  | 7522 | } else { | 
|  | 7523 | return false; | 
|  | 7524 | } | 
| Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7525 | } | 
|  | 7526 |  | 
|  | 7527 | if (FT->getNumParams() < NumActualArgs && !FT->isVarArg() && | 
| Reid Spencer | 5301e7c | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 7528 | Callee->isDeclaration()) | 
| Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7529 | return false;   // Do not delete arguments unless we have a function body... | 
|  | 7530 |  | 
|  | 7531 | // Okay, we decided that this is a safe thing to do: go ahead and start | 
|  | 7532 | // inserting cast instructions as necessary... | 
|  | 7533 | std::vector<Value*> Args; | 
|  | 7534 | Args.reserve(NumActualArgs); | 
|  | 7535 |  | 
|  | 7536 | AI = CS.arg_begin(); | 
|  | 7537 | for (unsigned i = 0; i != NumCommonArgs; ++i, ++AI) { | 
|  | 7538 | const Type *ParamTy = FT->getParamType(i); | 
|  | 7539 | if ((*AI)->getType() == ParamTy) { | 
|  | 7540 | Args.push_back(*AI); | 
|  | 7541 | } else { | 
| Reid Spencer | 668d90f | 2006-12-18 08:47:13 +0000 | [diff] [blame] | 7542 | Instruction::CastOps opcode = CastInst::getCastOpcode(*AI, | 
| Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 7543 | false, ParamTy, false); | 
| Reid Spencer | 668d90f | 2006-12-18 08:47:13 +0000 | [diff] [blame] | 7544 | CastInst *NewCast = CastInst::create(opcode, *AI, ParamTy, "tmp"); | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7545 | Args.push_back(InsertNewInstBefore(NewCast, *Caller)); | 
| Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7546 | } | 
|  | 7547 | } | 
|  | 7548 |  | 
|  | 7549 | // If the function takes more arguments than the call was taking, add them | 
|  | 7550 | // now... | 
|  | 7551 | for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i) | 
|  | 7552 | Args.push_back(Constant::getNullValue(FT->getParamType(i))); | 
|  | 7553 |  | 
|  | 7554 | // If we are removing arguments to the function, emit an obnoxious warning... | 
|  | 7555 | if (FT->getNumParams() < NumActualArgs) | 
|  | 7556 | if (!FT->isVarArg()) { | 
| Bill Wendling | f3baad3 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 7557 | cerr << "WARNING: While resolving call to function '" | 
|  | 7558 | << Callee->getName() << "' arguments were dropped!\n"; | 
| Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7559 | } else { | 
|  | 7560 | // Add all of the arguments in their promoted form to the arg list... | 
|  | 7561 | for (unsigned i = FT->getNumParams(); i != NumActualArgs; ++i, ++AI) { | 
|  | 7562 | const Type *PTy = getPromotedType((*AI)->getType()); | 
|  | 7563 | if (PTy != (*AI)->getType()) { | 
|  | 7564 | // Must promote to pass through va_arg area! | 
| Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 7565 | Instruction::CastOps opcode = CastInst::getCastOpcode(*AI, false, | 
|  | 7566 | PTy, false); | 
| Reid Spencer | 668d90f | 2006-12-18 08:47:13 +0000 | [diff] [blame] | 7567 | Instruction *Cast = CastInst::create(opcode, *AI, PTy, "tmp"); | 
| Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7568 | InsertNewInstBefore(Cast, *Caller); | 
|  | 7569 | Args.push_back(Cast); | 
|  | 7570 | } else { | 
|  | 7571 | Args.push_back(*AI); | 
|  | 7572 | } | 
|  | 7573 | } | 
|  | 7574 | } | 
|  | 7575 |  | 
|  | 7576 | if (FT->getReturnType() == Type::VoidTy) | 
| Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 7577 | Caller->setName("");   // Void type should not have a name. | 
| Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7578 |  | 
|  | 7579 | Instruction *NC; | 
|  | 7580 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { | 
| Chris Lattner | fae8ab3 | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 7581 | NC = new InvokeInst(Callee, II->getNormalDest(), II->getUnwindDest(), | 
| Chris Lattner | a06a8fd | 2007-02-13 02:10:56 +0000 | [diff] [blame] | 7582 | &Args[0], Args.size(), Caller->getName(), Caller); | 
| Chris Lattner | 05c703e | 2005-05-14 12:25:32 +0000 | [diff] [blame] | 7583 | cast<InvokeInst>(II)->setCallingConv(II->getCallingConv()); | 
| Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7584 | } else { | 
| Chris Lattner | a06a8fd | 2007-02-13 02:10:56 +0000 | [diff] [blame] | 7585 | NC = new CallInst(Callee, &Args[0], Args.size(), Caller->getName(), Caller); | 
| Chris Lattner | 6aacb0f | 2005-05-06 06:48:21 +0000 | [diff] [blame] | 7586 | if (cast<CallInst>(Caller)->isTailCall()) | 
|  | 7587 | cast<CallInst>(NC)->setTailCall(); | 
| Chris Lattner | 05c703e | 2005-05-14 12:25:32 +0000 | [diff] [blame] | 7588 | cast<CallInst>(NC)->setCallingConv(cast<CallInst>(Caller)->getCallingConv()); | 
| Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7589 | } | 
|  | 7590 |  | 
| Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 7591 | // Insert a cast of the return type as necessary. | 
| Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7592 | Value *NV = NC; | 
|  | 7593 | if (Caller->getType() != NV->getType() && !Caller->use_empty()) { | 
|  | 7594 | if (NV->getType() != Type::VoidTy) { | 
| Reid Spencer | 668d90f | 2006-12-18 08:47:13 +0000 | [diff] [blame] | 7595 | const Type *CallerTy = Caller->getType(); | 
| Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 7596 | Instruction::CastOps opcode = CastInst::getCastOpcode(NC, false, | 
|  | 7597 | CallerTy, false); | 
| Reid Spencer | 668d90f | 2006-12-18 08:47:13 +0000 | [diff] [blame] | 7598 | NV = NC = CastInst::create(opcode, NC, CallerTy, "tmp"); | 
| Chris Lattner | 686767f | 2003-10-30 00:46:41 +0000 | [diff] [blame] | 7599 |  | 
|  | 7600 | // If this is an invoke instruction, we should insert it after the first | 
|  | 7601 | // non-phi, instruction in the normal successor block. | 
|  | 7602 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { | 
|  | 7603 | BasicBlock::iterator I = II->getNormalDest()->begin(); | 
|  | 7604 | while (isa<PHINode>(I)) ++I; | 
|  | 7605 | InsertNewInstBefore(NC, *I); | 
|  | 7606 | } else { | 
|  | 7607 | // Otherwise, it's a call, just insert cast right after the call instr | 
|  | 7608 | InsertNewInstBefore(NC, *Caller); | 
|  | 7609 | } | 
| Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 7610 | AddUsersToWorkList(*Caller); | 
| Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7611 | } else { | 
| Chris Lattner | e29d634 | 2004-10-17 21:22:38 +0000 | [diff] [blame] | 7612 | NV = UndefValue::get(Caller->getType()); | 
| Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7613 | } | 
|  | 7614 | } | 
|  | 7615 |  | 
|  | 7616 | if (Caller->getType() != Type::VoidTy && !Caller->use_empty()) | 
|  | 7617 | Caller->replaceAllUsesWith(NV); | 
| Chris Lattner | 51f5457 | 2007-03-02 19:59:19 +0000 | [diff] [blame] | 7618 | Caller->eraseFromParent(); | 
| Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 7619 | RemoveFromWorkList(Caller); | 
| Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7620 | return true; | 
|  | 7621 | } | 
|  | 7622 |  | 
| Chris Lattner | cadac0c | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 7623 | /// FoldPHIArgBinOpIntoPHI - If we have something like phi [add (a,b), add(c,d)] | 
|  | 7624 | /// and if a/b/c/d and the add's all have a single use, turn this into two phi's | 
|  | 7625 | /// and a single binop. | 
|  | 7626 | Instruction *InstCombiner::FoldPHIArgBinOpIntoPHI(PHINode &PN) { | 
|  | 7627 | Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0)); | 
| Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7628 | assert(isa<BinaryOperator>(FirstInst) || isa<GetElementPtrInst>(FirstInst) || | 
|  | 7629 | isa<CmpInst>(FirstInst)); | 
| Chris Lattner | cadac0c | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 7630 | unsigned Opc = FirstInst->getOpcode(); | 
| Chris Lattner | cd62f11 | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 7631 | Value *LHSVal = FirstInst->getOperand(0); | 
|  | 7632 | Value *RHSVal = FirstInst->getOperand(1); | 
|  | 7633 |  | 
|  | 7634 | const Type *LHSType = LHSVal->getType(); | 
|  | 7635 | const Type *RHSType = RHSVal->getType(); | 
| Chris Lattner | cadac0c | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 7636 |  | 
|  | 7637 | // Scan to see if all operands are the same opcode, all have one use, and all | 
|  | 7638 | // kill their operands (i.e. the operands have one use). | 
| Chris Lattner | dc826fc | 2006-11-01 04:55:47 +0000 | [diff] [blame] | 7639 | for (unsigned i = 0; i != PN.getNumIncomingValues(); ++i) { | 
| Chris Lattner | cadac0c | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 7640 | Instruction *I = dyn_cast<Instruction>(PN.getIncomingValue(i)); | 
| Chris Lattner | dc826fc | 2006-11-01 04:55:47 +0000 | [diff] [blame] | 7641 | if (!I || I->getOpcode() != Opc || !I->hasOneUse() || | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7642 | // Verify type of the LHS matches so we don't fold cmp's of different | 
| Chris Lattner | eebea43 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 7643 | // types or GEP's with different index types. | 
|  | 7644 | I->getOperand(0)->getType() != LHSType || | 
|  | 7645 | I->getOperand(1)->getType() != RHSType) | 
| Chris Lattner | cadac0c | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 7646 | return 0; | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7647 |  | 
|  | 7648 | // If they are CmpInst instructions, check their predicates | 
|  | 7649 | if (Opc == Instruction::ICmp || Opc == Instruction::FCmp) | 
|  | 7650 | if (cast<CmpInst>(I)->getPredicate() != | 
|  | 7651 | cast<CmpInst>(FirstInst)->getPredicate()) | 
|  | 7652 | return 0; | 
| Chris Lattner | cd62f11 | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 7653 |  | 
|  | 7654 | // Keep track of which operand needs a phi node. | 
|  | 7655 | if (I->getOperand(0) != LHSVal) LHSVal = 0; | 
|  | 7656 | if (I->getOperand(1) != RHSVal) RHSVal = 0; | 
| Chris Lattner | cadac0c | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 7657 | } | 
|  | 7658 |  | 
| Chris Lattner | 4f218d5 | 2006-11-08 19:42:28 +0000 | [diff] [blame] | 7659 | // Otherwise, this is safe to transform, determine if it is profitable. | 
|  | 7660 |  | 
|  | 7661 | // If this is a GEP, and if the index (not the pointer) needs a PHI, bail out. | 
|  | 7662 | // Indexes are often folded into load/store instructions, so we don't want to | 
|  | 7663 | // hide them behind a phi. | 
|  | 7664 | if (isa<GetElementPtrInst>(FirstInst) && RHSVal == 0) | 
|  | 7665 | return 0; | 
|  | 7666 |  | 
| Chris Lattner | cadac0c | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 7667 | Value *InLHS = FirstInst->getOperand(0); | 
| Chris Lattner | cadac0c | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 7668 | Value *InRHS = FirstInst->getOperand(1); | 
| Chris Lattner | 4f218d5 | 2006-11-08 19:42:28 +0000 | [diff] [blame] | 7669 | PHINode *NewLHS = 0, *NewRHS = 0; | 
| Chris Lattner | cd62f11 | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 7670 | if (LHSVal == 0) { | 
|  | 7671 | NewLHS = new PHINode(LHSType, FirstInst->getOperand(0)->getName()+".pn"); | 
|  | 7672 | NewLHS->reserveOperandSpace(PN.getNumOperands()/2); | 
|  | 7673 | NewLHS->addIncoming(InLHS, PN.getIncomingBlock(0)); | 
| Chris Lattner | eebea43 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 7674 | InsertNewInstBefore(NewLHS, PN); | 
|  | 7675 | LHSVal = NewLHS; | 
|  | 7676 | } | 
| Chris Lattner | cd62f11 | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 7677 |  | 
|  | 7678 | if (RHSVal == 0) { | 
|  | 7679 | NewRHS = new PHINode(RHSType, FirstInst->getOperand(1)->getName()+".pn"); | 
|  | 7680 | NewRHS->reserveOperandSpace(PN.getNumOperands()/2); | 
|  | 7681 | NewRHS->addIncoming(InRHS, PN.getIncomingBlock(0)); | 
| Chris Lattner | eebea43 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 7682 | InsertNewInstBefore(NewRHS, PN); | 
|  | 7683 | RHSVal = NewRHS; | 
|  | 7684 | } | 
|  | 7685 |  | 
| Chris Lattner | cd62f11 | 2006-11-08 19:29:23 +0000 | [diff] [blame] | 7686 | // Add all operands to the new PHIs. | 
|  | 7687 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { | 
|  | 7688 | if (NewLHS) { | 
|  | 7689 | Value *NewInLHS =cast<Instruction>(PN.getIncomingValue(i))->getOperand(0); | 
|  | 7690 | NewLHS->addIncoming(NewInLHS, PN.getIncomingBlock(i)); | 
|  | 7691 | } | 
|  | 7692 | if (NewRHS) { | 
|  | 7693 | Value *NewInRHS =cast<Instruction>(PN.getIncomingValue(i))->getOperand(1); | 
|  | 7694 | NewRHS->addIncoming(NewInRHS, PN.getIncomingBlock(i)); | 
|  | 7695 | } | 
|  | 7696 | } | 
|  | 7697 |  | 
| Chris Lattner | cadac0c | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 7698 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst)) | 
| Chris Lattner | eebea43 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 7699 | return BinaryOperator::create(BinOp->getOpcode(), LHSVal, RHSVal); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7700 | else if (CmpInst *CIOp = dyn_cast<CmpInst>(FirstInst)) | 
|  | 7701 | return CmpInst::create(CIOp->getOpcode(), CIOp->getPredicate(), LHSVal, | 
|  | 7702 | RHSVal); | 
| Chris Lattner | eebea43 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 7703 | else { | 
|  | 7704 | assert(isa<GetElementPtrInst>(FirstInst)); | 
|  | 7705 | return new GetElementPtrInst(LHSVal, RHSVal); | 
|  | 7706 | } | 
| Chris Lattner | cadac0c | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 7707 | } | 
|  | 7708 |  | 
| Chris Lattner | 14f82c7 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 7709 | /// isSafeToSinkLoad - Return true if we know that it is safe sink the load out | 
|  | 7710 | /// of the block that defines it.  This means that it must be obvious the value | 
|  | 7711 | /// of the load is not changed from the point of the load to the end of the | 
|  | 7712 | /// block it is in. | 
| Chris Lattner | c904205 | 2007-02-01 22:30:07 +0000 | [diff] [blame] | 7713 | /// | 
|  | 7714 | /// Finally, it is safe, but not profitable, to sink a load targetting a | 
|  | 7715 | /// non-address-taken alloca.  Doing so will cause us to not promote the alloca | 
|  | 7716 | /// to a register. | 
| Chris Lattner | 14f82c7 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 7717 | static bool isSafeToSinkLoad(LoadInst *L) { | 
|  | 7718 | BasicBlock::iterator BBI = L, E = L->getParent()->end(); | 
|  | 7719 |  | 
|  | 7720 | for (++BBI; BBI != E; ++BBI) | 
|  | 7721 | if (BBI->mayWriteToMemory()) | 
|  | 7722 | return false; | 
| Chris Lattner | c904205 | 2007-02-01 22:30:07 +0000 | [diff] [blame] | 7723 |  | 
|  | 7724 | // Check for non-address taken alloca.  If not address-taken already, it isn't | 
|  | 7725 | // profitable to do this xform. | 
|  | 7726 | if (AllocaInst *AI = dyn_cast<AllocaInst>(L->getOperand(0))) { | 
|  | 7727 | bool isAddressTaken = false; | 
|  | 7728 | for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end(); | 
|  | 7729 | UI != E; ++UI) { | 
|  | 7730 | if (isa<LoadInst>(UI)) continue; | 
|  | 7731 | if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) { | 
|  | 7732 | // If storing TO the alloca, then the address isn't taken. | 
|  | 7733 | if (SI->getOperand(1) == AI) continue; | 
|  | 7734 | } | 
|  | 7735 | isAddressTaken = true; | 
|  | 7736 | break; | 
|  | 7737 | } | 
|  | 7738 |  | 
|  | 7739 | if (!isAddressTaken) | 
|  | 7740 | return false; | 
|  | 7741 | } | 
|  | 7742 |  | 
| Chris Lattner | 14f82c7 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 7743 | return true; | 
|  | 7744 | } | 
|  | 7745 |  | 
| Chris Lattner | 970c33a | 2003-06-19 17:00:31 +0000 | [diff] [blame] | 7746 |  | 
| Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 7747 | // FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary" | 
|  | 7748 | // operator and they all are only used by the PHI, PHI together their | 
|  | 7749 | // inputs, and do the operation once, to the result of the PHI. | 
|  | 7750 | Instruction *InstCombiner::FoldPHIArgOpIntoPHI(PHINode &PN) { | 
|  | 7751 | Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0)); | 
|  | 7752 |  | 
|  | 7753 | // Scan the instruction, looking for input operations that can be folded away. | 
|  | 7754 | // If all input operands to the phi are the same instruction (e.g. a cast from | 
|  | 7755 | // the same type or "+42") we can pull the operation through the PHI, reducing | 
|  | 7756 | // code size and simplifying code. | 
|  | 7757 | Constant *ConstantOp = 0; | 
|  | 7758 | const Type *CastSrcTy = 0; | 
| Chris Lattner | 14f82c7 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 7759 | bool isVolatile = false; | 
| Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 7760 | if (isa<CastInst>(FirstInst)) { | 
|  | 7761 | CastSrcTy = FirstInst->getOperand(0)->getType(); | 
| Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7762 | } else if (isa<BinaryOperator>(FirstInst) || isa<CmpInst>(FirstInst)) { | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7763 | // Can fold binop, compare or shift here if the RHS is a constant, | 
|  | 7764 | // otherwise call FoldPHIArgBinOpIntoPHI. | 
| Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 7765 | ConstantOp = dyn_cast<Constant>(FirstInst->getOperand(1)); | 
| Chris Lattner | cadac0c | 2006-11-01 04:51:18 +0000 | [diff] [blame] | 7766 | if (ConstantOp == 0) | 
|  | 7767 | return FoldPHIArgBinOpIntoPHI(PN); | 
| Chris Lattner | 14f82c7 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 7768 | } else if (LoadInst *LI = dyn_cast<LoadInst>(FirstInst)) { | 
|  | 7769 | isVolatile = LI->isVolatile(); | 
|  | 7770 | // We can't sink the load if the loaded value could be modified between the | 
|  | 7771 | // load and the PHI. | 
|  | 7772 | if (LI->getParent() != PN.getIncomingBlock(0) || | 
|  | 7773 | !isSafeToSinkLoad(LI)) | 
|  | 7774 | return 0; | 
| Chris Lattner | eebea43 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 7775 | } else if (isa<GetElementPtrInst>(FirstInst)) { | 
| Chris Lattner | 4f218d5 | 2006-11-08 19:42:28 +0000 | [diff] [blame] | 7776 | if (FirstInst->getNumOperands() == 2) | 
| Chris Lattner | eebea43 | 2006-11-01 07:43:41 +0000 | [diff] [blame] | 7777 | return FoldPHIArgBinOpIntoPHI(PN); | 
|  | 7778 | // Can't handle general GEPs yet. | 
|  | 7779 | return 0; | 
| Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 7780 | } else { | 
|  | 7781 | return 0;  // Cannot fold this operation. | 
|  | 7782 | } | 
|  | 7783 |  | 
|  | 7784 | // Check to see if all arguments are the same operation. | 
|  | 7785 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { | 
|  | 7786 | if (!isa<Instruction>(PN.getIncomingValue(i))) return 0; | 
|  | 7787 | Instruction *I = cast<Instruction>(PN.getIncomingValue(i)); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7788 | if (!I->hasOneUse() || !I->isSameOperationAs(FirstInst)) | 
| Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 7789 | return 0; | 
|  | 7790 | if (CastSrcTy) { | 
|  | 7791 | if (I->getOperand(0)->getType() != CastSrcTy) | 
|  | 7792 | return 0;  // Cast operation must match. | 
| Chris Lattner | 14f82c7 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 7793 | } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) { | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7794 | // We can't sink the load if the loaded value could be modified between | 
|  | 7795 | // the load and the PHI. | 
| Chris Lattner | 14f82c7 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 7796 | if (LI->isVolatile() != isVolatile || | 
|  | 7797 | LI->getParent() != PN.getIncomingBlock(i) || | 
|  | 7798 | !isSafeToSinkLoad(LI)) | 
|  | 7799 | return 0; | 
| Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 7800 | } else if (I->getOperand(1) != ConstantOp) { | 
|  | 7801 | return 0; | 
|  | 7802 | } | 
|  | 7803 | } | 
|  | 7804 |  | 
|  | 7805 | // Okay, they are all the same operation.  Create a new PHI node of the | 
|  | 7806 | // correct type, and PHI together all of the LHS's of the instructions. | 
|  | 7807 | PHINode *NewPN = new PHINode(FirstInst->getOperand(0)->getType(), | 
|  | 7808 | PN.getName()+".in"); | 
| Chris Lattner | d8e2018 | 2005-01-29 00:39:08 +0000 | [diff] [blame] | 7809 | NewPN->reserveOperandSpace(PN.getNumOperands()/2); | 
| Chris Lattner | 46dd5a6 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 7810 |  | 
|  | 7811 | Value *InVal = FirstInst->getOperand(0); | 
|  | 7812 | NewPN->addIncoming(InVal, PN.getIncomingBlock(0)); | 
| Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 7813 |  | 
|  | 7814 | // Add all operands to the new PHI. | 
| Chris Lattner | 46dd5a6 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 7815 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { | 
|  | 7816 | Value *NewInVal = cast<Instruction>(PN.getIncomingValue(i))->getOperand(0); | 
|  | 7817 | if (NewInVal != InVal) | 
|  | 7818 | InVal = 0; | 
|  | 7819 | NewPN->addIncoming(NewInVal, PN.getIncomingBlock(i)); | 
|  | 7820 | } | 
|  | 7821 |  | 
|  | 7822 | Value *PhiVal; | 
|  | 7823 | if (InVal) { | 
|  | 7824 | // The new PHI unions all of the same values together.  This is really | 
|  | 7825 | // common, so we handle it intelligently here for compile-time speed. | 
|  | 7826 | PhiVal = InVal; | 
|  | 7827 | delete NewPN; | 
|  | 7828 | } else { | 
|  | 7829 | InsertNewInstBefore(NewPN, PN); | 
|  | 7830 | PhiVal = NewPN; | 
|  | 7831 | } | 
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7832 |  | 
| Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 7833 | // Insert and return the new operation. | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 7834 | if (CastInst* FirstCI = dyn_cast<CastInst>(FirstInst)) | 
|  | 7835 | return CastInst::create(FirstCI->getOpcode(), PhiVal, PN.getType()); | 
| Reid Spencer | de46e48 | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 7836 | else if (isa<LoadInst>(FirstInst)) | 
| Chris Lattner | 14f82c7 | 2006-11-01 07:13:54 +0000 | [diff] [blame] | 7837 | return new LoadInst(PhiVal, "", isVolatile); | 
| Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 7838 | else if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst)) | 
| Chris Lattner | 46dd5a6 | 2004-11-14 19:29:34 +0000 | [diff] [blame] | 7839 | return BinaryOperator::create(BinOp->getOpcode(), PhiVal, ConstantOp); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7840 | else if (CmpInst *CIOp = dyn_cast<CmpInst>(FirstInst)) | 
|  | 7841 | return CmpInst::create(CIOp->getOpcode(), CIOp->getPredicate(), | 
|  | 7842 | PhiVal, ConstantOp); | 
| Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 7843 | else | 
| Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 7844 | assert(0 && "Unknown operation"); | 
| Jeff Cohen | b622c11 | 2007-03-05 00:00:42 +0000 | [diff] [blame] | 7845 | return 0; | 
| Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 7846 | } | 
| Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 7847 |  | 
| Chris Lattner | 7153643 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 7848 | /// DeadPHICycle - Return true if this PHI node is only used by a PHI node cycle | 
|  | 7849 | /// that is dead. | 
| Chris Lattner | d2602d5 | 2007-03-26 20:40:50 +0000 | [diff] [blame] | 7850 | static bool DeadPHICycle(PHINode *PN, | 
|  | 7851 | SmallPtrSet<PHINode*, 16> &PotentiallyDeadPHIs) { | 
| Chris Lattner | 7153643 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 7852 | if (PN->use_empty()) return true; | 
|  | 7853 | if (!PN->hasOneUse()) return false; | 
|  | 7854 |  | 
|  | 7855 | // Remember this node, and if we find the cycle, return. | 
| Chris Lattner | d2602d5 | 2007-03-26 20:40:50 +0000 | [diff] [blame] | 7856 | if (!PotentiallyDeadPHIs.insert(PN)) | 
| Chris Lattner | 7153643 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 7857 | return true; | 
|  | 7858 |  | 
|  | 7859 | if (PHINode *PU = dyn_cast<PHINode>(PN->use_back())) | 
|  | 7860 | return DeadPHICycle(PU, PotentiallyDeadPHIs); | 
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7861 |  | 
| Chris Lattner | 7153643 | 2005-01-17 05:10:15 +0000 | [diff] [blame] | 7862 | return false; | 
|  | 7863 | } | 
|  | 7864 |  | 
| Chris Lattner | bbbdd85 | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 7865 | // PHINode simplification | 
|  | 7866 | // | 
| Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 7867 | Instruction *InstCombiner::visitPHINode(PHINode &PN) { | 
| Owen Anderson | bbf8990 | 2006-07-10 22:15:25 +0000 | [diff] [blame] | 7868 | // If LCSSA is around, don't mess with Phi nodes | 
| Chris Lattner | 8258b44 | 2007-03-04 04:27:24 +0000 | [diff] [blame] | 7869 | if (MustPreserveLCSSA) return 0; | 
| Owen Anderson | a6968f8 | 2006-07-10 19:03:49 +0000 | [diff] [blame] | 7870 |  | 
| Owen Anderson | ae8aa64 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 7871 | if (Value *V = PN.hasConstantValue()) | 
|  | 7872 | return ReplaceInstUsesWith(PN, V); | 
|  | 7873 |  | 
| Owen Anderson | ae8aa64 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 7874 | // If all PHI operands are the same operation, pull them through the PHI, | 
|  | 7875 | // reducing code size. | 
|  | 7876 | if (isa<Instruction>(PN.getIncomingValue(0)) && | 
|  | 7877 | PN.getIncomingValue(0)->hasOneUse()) | 
|  | 7878 | if (Instruction *Result = FoldPHIArgOpIntoPHI(PN)) | 
|  | 7879 | return Result; | 
|  | 7880 |  | 
|  | 7881 | // If this is a trivial cycle in the PHI node graph, remove it.  Basically, if | 
|  | 7882 | // this PHI only has a single use (a PHI), and if that PHI only has one use (a | 
|  | 7883 | // PHI)... break the cycle. | 
| Chris Lattner | c8dcede | 2007-01-15 07:30:06 +0000 | [diff] [blame] | 7884 | if (PN.hasOneUse()) { | 
|  | 7885 | Instruction *PHIUser = cast<Instruction>(PN.use_back()); | 
|  | 7886 | if (PHINode *PU = dyn_cast<PHINode>(PHIUser)) { | 
| Chris Lattner | d2602d5 | 2007-03-26 20:40:50 +0000 | [diff] [blame] | 7887 | SmallPtrSet<PHINode*, 16> PotentiallyDeadPHIs; | 
| Owen Anderson | ae8aa64 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 7888 | PotentiallyDeadPHIs.insert(&PN); | 
|  | 7889 | if (DeadPHICycle(PU, PotentiallyDeadPHIs)) | 
|  | 7890 | return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType())); | 
|  | 7891 | } | 
| Chris Lattner | c8dcede | 2007-01-15 07:30:06 +0000 | [diff] [blame] | 7892 |  | 
|  | 7893 | // If this phi has a single use, and if that use just computes a value for | 
|  | 7894 | // the next iteration of a loop, delete the phi.  This occurs with unused | 
|  | 7895 | // induction variables, e.g. "for (int j = 0; ; ++j);".  Detecting this | 
|  | 7896 | // common case here is good because the only other things that catch this | 
|  | 7897 | // are induction variable analysis (sometimes) and ADCE, which is only run | 
|  | 7898 | // late. | 
|  | 7899 | if (PHIUser->hasOneUse() && | 
|  | 7900 | (isa<BinaryOperator>(PHIUser) || isa<GetElementPtrInst>(PHIUser)) && | 
|  | 7901 | PHIUser->use_back() == &PN) { | 
|  | 7902 | return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType())); | 
|  | 7903 | } | 
|  | 7904 | } | 
| Owen Anderson | ae8aa64 | 2006-07-10 22:03:18 +0000 | [diff] [blame] | 7905 |  | 
| Chris Lattner | 91daeb5 | 2003-12-19 05:58:40 +0000 | [diff] [blame] | 7906 | return 0; | 
| Chris Lattner | bbbdd85 | 2002-05-06 18:06:38 +0000 | [diff] [blame] | 7907 | } | 
|  | 7908 |  | 
| Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7909 | static Value *InsertCastToIntPtrTy(Value *V, const Type *DTy, | 
|  | 7910 | Instruction *InsertPoint, | 
|  | 7911 | InstCombiner *IC) { | 
| Reid Spencer | 8f166b0 | 2007-01-08 16:32:00 +0000 | [diff] [blame] | 7912 | unsigned PtrSize = DTy->getPrimitiveSizeInBits(); | 
|  | 7913 | unsigned VTySize = V->getType()->getPrimitiveSizeInBits(); | 
| Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7914 | // We must cast correctly to the pointer type. Ensure that we | 
|  | 7915 | // sign extend the integer value if it is smaller as this is | 
|  | 7916 | // used for address computation. | 
|  | 7917 | Instruction::CastOps opcode = | 
|  | 7918 | (VTySize < PtrSize ? Instruction::SExt : | 
|  | 7919 | (VTySize == PtrSize ? Instruction::BitCast : Instruction::Trunc)); | 
|  | 7920 | return IC->InsertCastBefore(opcode, V, DTy, *InsertPoint); | 
| Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7921 | } | 
|  | 7922 |  | 
| Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 7923 |  | 
| Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 7924 | Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { | 
| Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 7925 | Value *PtrOp = GEP.getOperand(0); | 
| Chris Lattner | 471bd76 | 2003-05-22 19:07:21 +0000 | [diff] [blame] | 7926 | // Is it 'getelementptr %P, long 0'  or 'getelementptr %P' | 
| Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 7927 | // If so, eliminate the noop. | 
| Chris Lattner | 8d0bacb | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 7928 | if (GEP.getNumOperands() == 1) | 
| Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 7929 | return ReplaceInstUsesWith(GEP, PtrOp); | 
| Chris Lattner | 8d0bacb | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 7930 |  | 
| Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 7931 | if (isa<UndefValue>(GEP.getOperand(0))) | 
|  | 7932 | return ReplaceInstUsesWith(GEP, UndefValue::get(GEP.getType())); | 
|  | 7933 |  | 
| Chris Lattner | 8d0bacb | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 7934 | bool HasZeroPointerIndex = false; | 
|  | 7935 | if (Constant *C = dyn_cast<Constant>(GEP.getOperand(1))) | 
|  | 7936 | HasZeroPointerIndex = C->isNullValue(); | 
|  | 7937 |  | 
|  | 7938 | if (GEP.getNumOperands() == 2 && HasZeroPointerIndex) | 
| Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 7939 | return ReplaceInstUsesWith(GEP, PtrOp); | 
| Chris Lattner | 48a44f7 | 2002-05-02 17:06:02 +0000 | [diff] [blame] | 7940 |  | 
| Chris Lattner | 9bf53ff | 2007-03-25 20:43:09 +0000 | [diff] [blame] | 7941 | // Keep track of whether all indices are zero constants integers. | 
|  | 7942 | bool AllZeroIndices = true; | 
|  | 7943 |  | 
| Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7944 | // Eliminate unneeded casts for indices. | 
|  | 7945 | bool MadeChange = false; | 
| Chris Lattner | 9bf53ff | 2007-03-25 20:43:09 +0000 | [diff] [blame] | 7946 |  | 
| Chris Lattner | 2b2412d | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 7947 | gep_type_iterator GTI = gep_type_begin(GEP); | 
| Chris Lattner | 9bf53ff | 2007-03-25 20:43:09 +0000 | [diff] [blame] | 7948 | for (unsigned i = 1, e = GEP.getNumOperands(); i != e; ++i, ++GTI) { | 
|  | 7949 | // Track whether this GEP has all zero indices, if so, it doesn't move the | 
|  | 7950 | // input pointer, it just changes its type. | 
|  | 7951 | if (AllZeroIndices) { | 
|  | 7952 | if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP.getOperand(i))) | 
|  | 7953 | AllZeroIndices = CI->isNullValue(); | 
|  | 7954 | else | 
|  | 7955 | AllZeroIndices = false; | 
|  | 7956 | } | 
| Chris Lattner | 2b2412d | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 7957 | if (isa<SequentialType>(*GTI)) { | 
|  | 7958 | if (CastInst *CI = dyn_cast<CastInst>(GEP.getOperand(i))) { | 
| Chris Lattner | 27df1db | 2007-01-15 07:02:54 +0000 | [diff] [blame] | 7959 | if (CI->getOpcode() == Instruction::ZExt || | 
|  | 7960 | CI->getOpcode() == Instruction::SExt) { | 
|  | 7961 | const Type *SrcTy = CI->getOperand(0)->getType(); | 
|  | 7962 | // We can eliminate a cast from i32 to i64 iff the target | 
|  | 7963 | // is a 32-bit pointer target. | 
|  | 7964 | if (SrcTy->getPrimitiveSizeInBits() >= TD->getPointerSizeInBits()) { | 
|  | 7965 | MadeChange = true; | 
|  | 7966 | GEP.setOperand(i, CI->getOperand(0)); | 
| Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7967 | } | 
|  | 7968 | } | 
|  | 7969 | } | 
| Chris Lattner | 2b2412d | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 7970 | // If we are using a wider index than needed for this platform, shrink it | 
|  | 7971 | // to what we need.  If the incoming value needs a cast instruction, | 
|  | 7972 | // insert it.  This explicit cast can make subsequent optimizations more | 
|  | 7973 | // obvious. | 
|  | 7974 | Value *Op = GEP.getOperand(i); | 
| Reid Spencer | 7a9c62b | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 7975 | if (TD->getTypeSize(Op->getType()) > TD->getPointerSize()) | 
| Chris Lattner | 1e9ac1a | 2004-04-17 18:16:10 +0000 | [diff] [blame] | 7976 | if (Constant *C = dyn_cast<Constant>(Op)) { | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 7977 | GEP.setOperand(i, ConstantExpr::getTrunc(C, TD->getIntPtrType())); | 
| Chris Lattner | 1e9ac1a | 2004-04-17 18:16:10 +0000 | [diff] [blame] | 7978 | MadeChange = true; | 
|  | 7979 | } else { | 
| Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 7980 | Op = InsertCastBefore(Instruction::Trunc, Op, TD->getIntPtrType(), | 
|  | 7981 | GEP); | 
| Chris Lattner | 2b2412d | 2004-04-07 18:38:20 +0000 | [diff] [blame] | 7982 | GEP.setOperand(i, Op); | 
|  | 7983 | MadeChange = true; | 
|  | 7984 | } | 
| Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7985 | } | 
| Chris Lattner | 9bf53ff | 2007-03-25 20:43:09 +0000 | [diff] [blame] | 7986 | } | 
| Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 7987 | if (MadeChange) return &GEP; | 
|  | 7988 |  | 
| Chris Lattner | 9bf53ff | 2007-03-25 20:43:09 +0000 | [diff] [blame] | 7989 | // If this GEP instruction doesn't move the pointer, and if the input operand | 
|  | 7990 | // is a bitcast of another pointer, just replace the GEP with a bitcast of the | 
|  | 7991 | // real input to the dest type. | 
|  | 7992 | if (AllZeroIndices && isa<BitCastInst>(GEP.getOperand(0))) | 
|  | 7993 | return new BitCastInst(cast<BitCastInst>(GEP.getOperand(0))->getOperand(0), | 
|  | 7994 | GEP.getType()); | 
|  | 7995 |  | 
| Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 7996 | // Combine Indices - If the source pointer to this getelementptr instruction | 
|  | 7997 | // is a getelementptr instruction, combine the indices of the two | 
|  | 7998 | // getelementptr instructions into a single instruction. | 
|  | 7999 | // | 
| Chris Lattner | af6094f | 2007-02-15 22:48:32 +0000 | [diff] [blame] | 8000 | SmallVector<Value*, 8> SrcGEPOperands; | 
| Chris Lattner | 0798af3 | 2005-01-13 20:14:25 +0000 | [diff] [blame] | 8001 | if (User *Src = dyn_castGetElementPtr(PtrOp)) | 
| Chris Lattner | af6094f | 2007-02-15 22:48:32 +0000 | [diff] [blame] | 8002 | SrcGEPOperands.append(Src->op_begin(), Src->op_end()); | 
| Chris Lattner | 57c67b0 | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 8003 |  | 
|  | 8004 | if (!SrcGEPOperands.empty()) { | 
| Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 8005 | // Note that if our source is a gep chain itself that we wait for that | 
|  | 8006 | // chain to be resolved before we perform this transformation.  This | 
|  | 8007 | // avoids us creating a TON of code in some cases. | 
|  | 8008 | // | 
|  | 8009 | if (isa<GetElementPtrInst>(SrcGEPOperands[0]) && | 
|  | 8010 | cast<Instruction>(SrcGEPOperands[0])->getNumOperands() == 2) | 
|  | 8011 | return 0;   // Wait until our source is folded to completion. | 
|  | 8012 |  | 
| Chris Lattner | af6094f | 2007-02-15 22:48:32 +0000 | [diff] [blame] | 8013 | SmallVector<Value*, 8> Indices; | 
| Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 8014 |  | 
|  | 8015 | // Find out whether the last index in the source GEP is a sequential idx. | 
|  | 8016 | bool EndsWithSequential = false; | 
|  | 8017 | for (gep_type_iterator I = gep_type_begin(*cast<User>(PtrOp)), | 
|  | 8018 | E = gep_type_end(*cast<User>(PtrOp)); I != E; ++I) | 
| Chris Lattner | 8ec5f88 | 2004-05-08 22:41:42 +0000 | [diff] [blame] | 8019 | EndsWithSequential = !isa<StructType>(*I); | 
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8020 |  | 
| Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 8021 | // Can we combine the two pointer arithmetics offsets? | 
| Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 8022 | if (EndsWithSequential) { | 
| Chris Lattner | 235af56 | 2003-03-05 22:33:14 +0000 | [diff] [blame] | 8023 | // Replace: gep (gep %P, long B), long A, ... | 
|  | 8024 | // With:    T = long A+B; gep %P, T, ... | 
|  | 8025 | // | 
| Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 8026 | Value *Sum, *SO1 = SrcGEPOperands.back(), *GO1 = GEP.getOperand(1); | 
| Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 8027 | if (SO1 == Constant::getNullValue(SO1->getType())) { | 
|  | 8028 | Sum = GO1; | 
|  | 8029 | } else if (GO1 == Constant::getNullValue(GO1->getType())) { | 
|  | 8030 | Sum = SO1; | 
|  | 8031 | } else { | 
|  | 8032 | // If they aren't the same type, convert both to an integer of the | 
|  | 8033 | // target's pointer size. | 
|  | 8034 | if (SO1->getType() != GO1->getType()) { | 
|  | 8035 | if (Constant *SO1C = dyn_cast<Constant>(SO1)) { | 
| Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 8036 | SO1 = ConstantExpr::getIntegerCast(SO1C, GO1->getType(), true); | 
| Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 8037 | } else if (Constant *GO1C = dyn_cast<Constant>(GO1)) { | 
| Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 8038 | GO1 = ConstantExpr::getIntegerCast(GO1C, SO1->getType(), true); | 
| Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 8039 | } else { | 
|  | 8040 | unsigned PS = TD->getPointerSize(); | 
| Reid Spencer | 7a9c62b | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 8041 | if (TD->getTypeSize(SO1->getType()) == PS) { | 
| Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 8042 | // Convert GO1 to SO1's type. | 
| Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 8043 | GO1 = InsertCastToIntPtrTy(GO1, SO1->getType(), &GEP, this); | 
| Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 8044 |  | 
| Reid Spencer | 7a9c62b | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 8045 | } else if (TD->getTypeSize(GO1->getType()) == PS) { | 
| Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 8046 | // Convert SO1 to GO1's type. | 
| Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 8047 | SO1 = InsertCastToIntPtrTy(SO1, GO1->getType(), &GEP, this); | 
| Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 8048 | } else { | 
|  | 8049 | const Type *PT = TD->getIntPtrType(); | 
| Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 8050 | SO1 = InsertCastToIntPtrTy(SO1, PT, &GEP, this); | 
|  | 8051 | GO1 = InsertCastToIntPtrTy(GO1, PT, &GEP, this); | 
| Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 8052 | } | 
|  | 8053 | } | 
|  | 8054 | } | 
| Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 8055 | if (isa<Constant>(SO1) && isa<Constant>(GO1)) | 
|  | 8056 | Sum = ConstantExpr::getAdd(cast<Constant>(SO1), cast<Constant>(GO1)); | 
|  | 8057 | else { | 
| Chris Lattner | df20a4d | 2004-06-10 02:07:29 +0000 | [diff] [blame] | 8058 | Sum = BinaryOperator::createAdd(SO1, GO1, PtrOp->getName()+".sum"); | 
|  | 8059 | InsertNewInstBefore(cast<Instruction>(Sum), GEP); | 
| Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 8060 | } | 
| Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 8061 | } | 
| Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 8062 |  | 
|  | 8063 | // Recycle the GEP we already have if possible. | 
|  | 8064 | if (SrcGEPOperands.size() == 2) { | 
|  | 8065 | GEP.setOperand(0, SrcGEPOperands[0]); | 
|  | 8066 | GEP.setOperand(1, Sum); | 
|  | 8067 | return &GEP; | 
|  | 8068 | } else { | 
|  | 8069 | Indices.insert(Indices.end(), SrcGEPOperands.begin()+1, | 
|  | 8070 | SrcGEPOperands.end()-1); | 
|  | 8071 | Indices.push_back(Sum); | 
|  | 8072 | Indices.insert(Indices.end(), GEP.op_begin()+2, GEP.op_end()); | 
|  | 8073 | } | 
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8074 | } else if (isa<Constant>(*GEP.idx_begin()) && | 
| Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 8075 | cast<Constant>(*GEP.idx_begin())->isNullValue() && | 
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8076 | SrcGEPOperands.size() != 1) { | 
| Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 8077 | // Otherwise we can do the fold if the first index of the GEP is a zero | 
| Chris Lattner | 57c67b0 | 2004-03-25 22:59:29 +0000 | [diff] [blame] | 8078 | Indices.insert(Indices.end(), SrcGEPOperands.begin()+1, | 
|  | 8079 | SrcGEPOperands.end()); | 
| Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 8080 | Indices.insert(Indices.end(), GEP.idx_begin()+1, GEP.idx_end()); | 
|  | 8081 | } | 
|  | 8082 |  | 
|  | 8083 | if (!Indices.empty()) | 
| Chris Lattner | a731513 | 2007-02-12 22:56:41 +0000 | [diff] [blame] | 8084 | return new GetElementPtrInst(SrcGEPOperands[0], &Indices[0], | 
|  | 8085 | Indices.size(), GEP.getName()); | 
| Chris Lattner | c59af1d | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 8086 |  | 
| Chris Lattner | 5f667a6 | 2004-05-07 22:09:22 +0000 | [diff] [blame] | 8087 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(PtrOp)) { | 
| Chris Lattner | c59af1d | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 8088 | // GEP of global variable.  If all of the indices for this GEP are | 
|  | 8089 | // constants, we can promote this to a constexpr instead of an instruction. | 
|  | 8090 |  | 
|  | 8091 | // Scan for nonconstants... | 
| Chris Lattner | f96f4a8 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 8092 | SmallVector<Constant*, 8> Indices; | 
| Chris Lattner | c59af1d | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 8093 | User::op_iterator I = GEP.idx_begin(), E = GEP.idx_end(); | 
|  | 8094 | for (; I != E && isa<Constant>(*I); ++I) | 
|  | 8095 | Indices.push_back(cast<Constant>(*I)); | 
|  | 8096 |  | 
|  | 8097 | if (I == E) {  // If they are all constants... | 
| Chris Lattner | f96f4a8 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 8098 | Constant *CE = ConstantExpr::getGetElementPtr(GV, | 
|  | 8099 | &Indices[0],Indices.size()); | 
| Chris Lattner | c59af1d | 2002-08-17 22:21:59 +0000 | [diff] [blame] | 8100 |  | 
|  | 8101 | // Replace all uses of the GEP with the new constexpr... | 
|  | 8102 | return ReplaceInstUsesWith(GEP, CE); | 
|  | 8103 | } | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8104 | } else if (Value *X = getBitCastOperand(PtrOp)) {  // Is the operand a cast? | 
| Chris Lattner | 567b81f | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 8105 | if (!isa<PointerType>(X->getType())) { | 
|  | 8106 | // Not interesting.  Source pointer must be a cast from pointer. | 
|  | 8107 | } else if (HasZeroPointerIndex) { | 
|  | 8108 | // transform: GEP (cast [10 x ubyte]* X to [0 x ubyte]*), long 0, ... | 
|  | 8109 | // into     : GEP [10 x ubyte]* X, long 0, ... | 
|  | 8110 | // | 
|  | 8111 | // This occurs when the program declares an array extern like "int X[];" | 
|  | 8112 | // | 
|  | 8113 | const PointerType *CPTy = cast<PointerType>(PtrOp->getType()); | 
|  | 8114 | const PointerType *XTy = cast<PointerType>(X->getType()); | 
|  | 8115 | if (const ArrayType *XATy = | 
|  | 8116 | dyn_cast<ArrayType>(XTy->getElementType())) | 
|  | 8117 | if (const ArrayType *CATy = | 
|  | 8118 | dyn_cast<ArrayType>(CPTy->getElementType())) | 
|  | 8119 | if (CATy->getElementType() == XATy->getElementType()) { | 
|  | 8120 | // At this point, we know that the cast source type is a pointer | 
|  | 8121 | // to an array of the same type as the destination pointer | 
|  | 8122 | // array.  Because the array type is never stepped over (there | 
|  | 8123 | // is a leading zero) we can fold the cast into this GEP. | 
|  | 8124 | GEP.setOperand(0, X); | 
|  | 8125 | return &GEP; | 
|  | 8126 | } | 
|  | 8127 | } else if (GEP.getNumOperands() == 2) { | 
|  | 8128 | // Transform things like: | 
| Chris Lattner | 2a89329 | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 8129 | // %t = getelementptr ubyte* cast ([2 x int]* %str to uint*), uint %V | 
|  | 8130 | // into:  %t1 = getelementptr [2 x int*]* %str, int 0, uint %V; cast | 
| Chris Lattner | 567b81f | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 8131 | const Type *SrcElTy = cast<PointerType>(X->getType())->getElementType(); | 
|  | 8132 | const Type *ResElTy=cast<PointerType>(PtrOp->getType())->getElementType(); | 
|  | 8133 | if (isa<ArrayType>(SrcElTy) && | 
|  | 8134 | TD->getTypeSize(cast<ArrayType>(SrcElTy)->getElementType()) == | 
|  | 8135 | TD->getTypeSize(ResElTy)) { | 
|  | 8136 | Value *V = InsertNewInstBefore( | 
| Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8137 | new GetElementPtrInst(X, Constant::getNullValue(Type::Int32Ty), | 
| Chris Lattner | 567b81f | 2005-09-13 00:40:14 +0000 | [diff] [blame] | 8138 | GEP.getOperand(1), GEP.getName()), GEP); | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8139 | // V and GEP are both pointer types --> BitCast | 
|  | 8140 | return new BitCastInst(V, GEP.getType()); | 
| Chris Lattner | 8d0bacb | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 8141 | } | 
| Chris Lattner | 2a89329 | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 8142 |  | 
|  | 8143 | // Transform things like: | 
|  | 8144 | // getelementptr sbyte* cast ([100 x double]* X to sbyte*), int %tmp | 
|  | 8145 | //   (where tmp = 8*tmp2) into: | 
|  | 8146 | // getelementptr [100 x double]* %arr, int 0, int %tmp.2 | 
|  | 8147 |  | 
|  | 8148 | if (isa<ArrayType>(SrcElTy) && | 
| Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8149 | (ResElTy == Type::Int8Ty || ResElTy == Type::Int8Ty)) { | 
| Chris Lattner | 2a89329 | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 8150 | uint64_t ArrayEltSize = | 
|  | 8151 | TD->getTypeSize(cast<ArrayType>(SrcElTy)->getElementType()); | 
|  | 8152 |  | 
|  | 8153 | // Check to see if "tmp" is a scale by a multiple of ArrayEltSize.  We | 
|  | 8154 | // allow either a mul, shift, or constant here. | 
|  | 8155 | Value *NewIdx = 0; | 
|  | 8156 | ConstantInt *Scale = 0; | 
|  | 8157 | if (ArrayEltSize == 1) { | 
|  | 8158 | NewIdx = GEP.getOperand(1); | 
|  | 8159 | Scale = ConstantInt::get(NewIdx->getType(), 1); | 
|  | 8160 | } else if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP.getOperand(1))) { | 
| Chris Lattner | a393e4d | 2005-09-14 17:32:56 +0000 | [diff] [blame] | 8161 | NewIdx = ConstantInt::get(CI->getType(), 1); | 
| Chris Lattner | 2a89329 | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 8162 | Scale = CI; | 
|  | 8163 | } else if (Instruction *Inst =dyn_cast<Instruction>(GEP.getOperand(1))){ | 
|  | 8164 | if (Inst->getOpcode() == Instruction::Shl && | 
|  | 8165 | isa<ConstantInt>(Inst->getOperand(1))) { | 
| Zhou Sheng | b25806f | 2007-03-30 09:29:48 +0000 | [diff] [blame] | 8166 | ConstantInt *ShAmt = cast<ConstantInt>(Inst->getOperand(1)); | 
|  | 8167 | uint32_t ShAmtVal = ShAmt->getLimitedValue(64); | 
|  | 8168 | Scale = ConstantInt::get(Inst->getType(), 1ULL << ShAmtVal); | 
| Chris Lattner | 2a89329 | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 8169 | NewIdx = Inst->getOperand(0); | 
|  | 8170 | } else if (Inst->getOpcode() == Instruction::Mul && | 
|  | 8171 | isa<ConstantInt>(Inst->getOperand(1))) { | 
|  | 8172 | Scale = cast<ConstantInt>(Inst->getOperand(1)); | 
|  | 8173 | NewIdx = Inst->getOperand(0); | 
|  | 8174 | } | 
|  | 8175 | } | 
|  | 8176 |  | 
|  | 8177 | // If the index will be to exactly the right offset with the scale taken | 
|  | 8178 | // out, perform the transformation. | 
| Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8179 | if (Scale && Scale->getZExtValue() % ArrayEltSize == 0) { | 
| Reid Spencer | de46e48 | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 8180 | if (isa<ConstantInt>(Scale)) | 
| Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8181 | Scale = ConstantInt::get(Scale->getType(), | 
|  | 8182 | Scale->getZExtValue() / ArrayEltSize); | 
|  | 8183 | if (Scale->getZExtValue() != 1) { | 
| Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 8184 | Constant *C = ConstantExpr::getIntegerCast(Scale, NewIdx->getType(), | 
|  | 8185 | true /*SExt*/); | 
| Chris Lattner | 2a89329 | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 8186 | Instruction *Sc = BinaryOperator::createMul(NewIdx, C, "idxscale"); | 
|  | 8187 | NewIdx = InsertNewInstBefore(Sc, GEP); | 
|  | 8188 | } | 
|  | 8189 |  | 
|  | 8190 | // Insert the new GEP instruction. | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8191 | Instruction *NewGEP = | 
| Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8192 | new GetElementPtrInst(X, Constant::getNullValue(Type::Int32Ty), | 
| Chris Lattner | 2a89329 | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 8193 | NewIdx, GEP.getName()); | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8194 | NewGEP = InsertNewInstBefore(NewGEP, GEP); | 
|  | 8195 | // The NewGEP must be pointer typed, so must the old one -> BitCast | 
|  | 8196 | return new BitCastInst(NewGEP, GEP.getType()); | 
| Chris Lattner | 2a89329 | 2005-09-13 18:36:04 +0000 | [diff] [blame] | 8197 | } | 
|  | 8198 | } | 
| Chris Lattner | 8d0bacb | 2004-02-22 05:25:17 +0000 | [diff] [blame] | 8199 | } | 
| Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 8200 | } | 
|  | 8201 |  | 
| Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 8202 | return 0; | 
|  | 8203 | } | 
|  | 8204 |  | 
| Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 8205 | Instruction *InstCombiner::visitAllocationInst(AllocationInst &AI) { | 
|  | 8206 | // Convert: malloc Ty, C - where C is a constant != 1 into: malloc [C x Ty], 1 | 
|  | 8207 | if (AI.isArrayAllocation())    // Check C != 1 | 
| Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8208 | if (const ConstantInt *C = dyn_cast<ConstantInt>(AI.getArraySize())) { | 
|  | 8209 | const Type *NewTy = | 
|  | 8210 | ArrayType::get(AI.getAllocatedType(), C->getZExtValue()); | 
| Chris Lattner | a2620ac | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 8211 | AllocationInst *New = 0; | 
| Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 8212 |  | 
|  | 8213 | // Create and insert the replacement instruction... | 
|  | 8214 | if (isa<MallocInst>(AI)) | 
| Nate Begeman | 848622f | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 8215 | New = new MallocInst(NewTy, 0, AI.getAlignment(), AI.getName()); | 
| Chris Lattner | a2620ac | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 8216 | else { | 
|  | 8217 | assert(isa<AllocaInst>(AI) && "Unknown type of allocation inst!"); | 
| Nate Begeman | 848622f | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 8218 | New = new AllocaInst(NewTy, 0, AI.getAlignment(), AI.getName()); | 
| Chris Lattner | a2620ac | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 8219 | } | 
| Chris Lattner | abb77c9 | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 8220 |  | 
|  | 8221 | InsertNewInstBefore(New, AI); | 
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8222 |  | 
| Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 8223 | // Scan to the end of the allocation instructions, to skip over a block of | 
|  | 8224 | // allocas if possible... | 
|  | 8225 | // | 
|  | 8226 | BasicBlock::iterator It = New; | 
|  | 8227 | while (isa<AllocationInst>(*It)) ++It; | 
|  | 8228 |  | 
|  | 8229 | // Now that I is pointing to the first non-allocation-inst in the block, | 
|  | 8230 | // insert our getelementptr instruction... | 
|  | 8231 | // | 
| Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8232 | Value *NullIdx = Constant::getNullValue(Type::Int32Ty); | 
| Chris Lattner | 809dfac | 2005-05-04 19:10:26 +0000 | [diff] [blame] | 8233 | Value *V = new GetElementPtrInst(New, NullIdx, NullIdx, | 
|  | 8234 | New->getName()+".sub", It); | 
| Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 8235 |  | 
|  | 8236 | // Now make everything use the getelementptr instead of the original | 
|  | 8237 | // allocation. | 
| Chris Lattner | abb77c9 | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 8238 | return ReplaceInstUsesWith(AI, V); | 
| Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8239 | } else if (isa<UndefValue>(AI.getArraySize())) { | 
|  | 8240 | return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType())); | 
| Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 8241 | } | 
| Chris Lattner | abb77c9 | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 8242 |  | 
|  | 8243 | // If alloca'ing a zero byte object, replace the alloca with a null pointer. | 
|  | 8244 | // Note that we only do this for alloca's, because malloc should allocate and | 
|  | 8245 | // return a unique pointer, even for a zero byte allocation. | 
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8246 | if (isa<AllocaInst>(AI) && AI.getAllocatedType()->isSized() && | 
| Chris Lattner | 49df6ce | 2004-07-02 22:55:47 +0000 | [diff] [blame] | 8247 | TD->getTypeSize(AI.getAllocatedType()) == 0) | 
| Chris Lattner | abb77c9 | 2004-03-19 06:08:10 +0000 | [diff] [blame] | 8248 | return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType())); | 
|  | 8249 |  | 
| Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 8250 | return 0; | 
|  | 8251 | } | 
|  | 8252 |  | 
| Chris Lattner | 8427bff | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 8253 | Instruction *InstCombiner::visitFreeInst(FreeInst &FI) { | 
|  | 8254 | Value *Op = FI.getOperand(0); | 
|  | 8255 |  | 
|  | 8256 | // Change free <ty>* (cast <ty2>* X to <ty>*) into free <ty2>* X | 
|  | 8257 | if (CastInst *CI = dyn_cast<CastInst>(Op)) | 
|  | 8258 | if (isa<PointerType>(CI->getOperand(0)->getType())) { | 
|  | 8259 | FI.setOperand(0, CI->getOperand(0)); | 
|  | 8260 | return &FI; | 
|  | 8261 | } | 
|  | 8262 |  | 
| Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 8263 | // free undef -> unreachable. | 
|  | 8264 | if (isa<UndefValue>(Op)) { | 
|  | 8265 | // Insert a new store to null because we cannot modify the CFG here. | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 8266 | new StoreInst(ConstantInt::getTrue(), | 
| Reid Spencer | 542964f | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 8267 | UndefValue::get(PointerType::get(Type::Int1Ty)), &FI); | 
| Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 8268 | return EraseInstFromFunction(FI); | 
|  | 8269 | } | 
|  | 8270 |  | 
| Chris Lattner | f3a3660 | 2004-02-28 04:57:37 +0000 | [diff] [blame] | 8271 | // If we have 'free null' delete the instruction.  This can happen in stl code | 
|  | 8272 | // when lots of inlining happens. | 
| Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 8273 | if (isa<ConstantPointerNull>(Op)) | 
| Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 8274 | return EraseInstFromFunction(FI); | 
| Chris Lattner | f3a3660 | 2004-02-28 04:57:37 +0000 | [diff] [blame] | 8275 |  | 
| Chris Lattner | 8427bff | 2003-12-07 01:24:23 +0000 | [diff] [blame] | 8276 | return 0; | 
|  | 8277 | } | 
|  | 8278 |  | 
|  | 8279 |  | 
| Chris Lattner | 72684fe | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8280 | /// InstCombineLoadCast - Fold 'load (cast P)' -> cast (load P)' when possible. | 
| Chris Lattner | 35e2477 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 8281 | static Instruction *InstCombineLoadCast(InstCombiner &IC, LoadInst &LI) { | 
|  | 8282 | User *CI = cast<User>(LI.getOperand(0)); | 
| Chris Lattner | fe1b0b8 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 8283 | Value *CastOp = CI->getOperand(0); | 
| Chris Lattner | 35e2477 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 8284 |  | 
|  | 8285 | const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType(); | 
| Chris Lattner | fe1b0b8 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 8286 | if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) { | 
| Chris Lattner | 35e2477 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 8287 | const Type *SrcPTy = SrcTy->getElementType(); | 
| Chris Lattner | fe1b0b8 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 8288 |  | 
| Reid Spencer | 31a4ef4 | 2007-01-22 05:51:25 +0000 | [diff] [blame] | 8289 | if (DestPTy->isInteger() || isa<PointerType>(DestPTy) || | 
| Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 8290 | isa<VectorType>(DestPTy)) { | 
| Chris Lattner | fe1b0b8 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 8291 | // If the source is an array, the code below will not succeed.  Check to | 
|  | 8292 | // see if a trivial 'gep P, 0, 0' will help matters.  Only do this for | 
|  | 8293 | // constants. | 
|  | 8294 | if (const ArrayType *ASrcTy = dyn_cast<ArrayType>(SrcPTy)) | 
|  | 8295 | if (Constant *CSrc = dyn_cast<Constant>(CastOp)) | 
|  | 8296 | if (ASrcTy->getNumElements() != 0) { | 
| Chris Lattner | f96f4a8 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 8297 | Value *Idxs[2]; | 
|  | 8298 | Idxs[0] = Idxs[1] = Constant::getNullValue(Type::Int32Ty); | 
|  | 8299 | CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs, 2); | 
| Chris Lattner | fe1b0b8 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 8300 | SrcTy = cast<PointerType>(CastOp->getType()); | 
|  | 8301 | SrcPTy = SrcTy->getElementType(); | 
|  | 8302 | } | 
|  | 8303 |  | 
| Reid Spencer | 31a4ef4 | 2007-01-22 05:51:25 +0000 | [diff] [blame] | 8304 | if ((SrcPTy->isInteger() || isa<PointerType>(SrcPTy) || | 
| Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 8305 | isa<VectorType>(SrcPTy)) && | 
| Chris Lattner | ecfa9b5 | 2005-03-29 06:37:47 +0000 | [diff] [blame] | 8306 | // Do not allow turning this into a load of an integer, which is then | 
|  | 8307 | // casted to a pointer, this pessimizes pointer analysis a lot. | 
|  | 8308 | (isa<PointerType>(SrcPTy) == isa<PointerType>(LI.getType())) && | 
| Reid Spencer | 31a4ef4 | 2007-01-22 05:51:25 +0000 | [diff] [blame] | 8309 | IC.getTargetData().getTypeSizeInBits(SrcPTy) == | 
|  | 8310 | IC.getTargetData().getTypeSizeInBits(DestPTy)) { | 
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8311 |  | 
| Chris Lattner | fe1b0b8 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 8312 | // Okay, we are casting from one integer or pointer type to another of | 
|  | 8313 | // the same size.  Instead of casting the pointer before the load, cast | 
|  | 8314 | // the result of the loaded value. | 
|  | 8315 | Value *NewLoad = IC.InsertNewInstBefore(new LoadInst(CastOp, | 
|  | 8316 | CI->getName(), | 
|  | 8317 | LI.isVolatile()),LI); | 
|  | 8318 | // Now cast the result of the load. | 
| Reid Spencer | bb65ebf | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 8319 | return new BitCastInst(NewLoad, LI.getType()); | 
| Chris Lattner | fe1b0b8 | 2005-01-31 04:50:46 +0000 | [diff] [blame] | 8320 | } | 
| Chris Lattner | 35e2477 | 2004-07-13 01:49:43 +0000 | [diff] [blame] | 8321 | } | 
|  | 8322 | } | 
|  | 8323 | return 0; | 
|  | 8324 | } | 
|  | 8325 |  | 
| Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 8326 | /// isSafeToLoadUnconditionally - Return true if we know that executing a load | 
| Chris Lattner | e6f1309 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 8327 | /// from this value cannot trap.  If it is not obviously safe to load from the | 
|  | 8328 | /// specified pointer, we do a quick local scan of the basic block containing | 
|  | 8329 | /// ScanFrom, to determine if the address is already accessed. | 
|  | 8330 | static bool isSafeToLoadUnconditionally(Value *V, Instruction *ScanFrom) { | 
|  | 8331 | // If it is an alloca or global variable, it is always safe to load from. | 
|  | 8332 | if (isa<AllocaInst>(V) || isa<GlobalVariable>(V)) return true; | 
|  | 8333 |  | 
|  | 8334 | // Otherwise, be a little bit agressive by scanning the local block where we | 
|  | 8335 | // want to check to see if the pointer is already being loaded or stored | 
| Alkis Evlogimenos | d59cebf | 2004-09-20 06:42:58 +0000 | [diff] [blame] | 8336 | // from/to.  If so, the previous load or store would have already trapped, | 
|  | 8337 | // so there is no harm doing an extra load (also, CSE will later eliminate | 
|  | 8338 | // the load entirely). | 
| Chris Lattner | e6f1309 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 8339 | BasicBlock::iterator BBI = ScanFrom, E = ScanFrom->getParent()->begin(); | 
|  | 8340 |  | 
| Alkis Evlogimenos | d59cebf | 2004-09-20 06:42:58 +0000 | [diff] [blame] | 8341 | while (BBI != E) { | 
| Chris Lattner | e6f1309 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 8342 | --BBI; | 
|  | 8343 |  | 
|  | 8344 | if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) { | 
|  | 8345 | if (LI->getOperand(0) == V) return true; | 
|  | 8346 | } else if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) | 
|  | 8347 | if (SI->getOperand(1) == V) return true; | 
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8348 |  | 
| Alkis Evlogimenos | d59cebf | 2004-09-20 06:42:58 +0000 | [diff] [blame] | 8349 | } | 
| Chris Lattner | e6f1309 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 8350 | return false; | 
| Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 8351 | } | 
|  | 8352 |  | 
| Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 8353 | Instruction *InstCombiner::visitLoadInst(LoadInst &LI) { | 
|  | 8354 | Value *Op = LI.getOperand(0); | 
| Chris Lattner | 7e8af38 | 2004-01-12 04:13:56 +0000 | [diff] [blame] | 8355 |  | 
| Chris Lattner | a9d84e3 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 8356 | // load (cast X) --> cast (load X) iff safe | 
| Reid Spencer | de46e48 | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 8357 | if (isa<CastInst>(Op)) | 
| Chris Lattner | a9d84e3 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 8358 | if (Instruction *Res = InstCombineLoadCast(*this, LI)) | 
|  | 8359 | return Res; | 
|  | 8360 |  | 
|  | 8361 | // None of the following transforms are legal for volatile loads. | 
|  | 8362 | if (LI.isVolatile()) return 0; | 
| Chris Lattner | b990f7d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 8363 |  | 
| Chris Lattner | b990f7d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 8364 | if (&LI.getParent()->front() != &LI) { | 
|  | 8365 | BasicBlock::iterator BBI = &LI; --BBI; | 
| Chris Lattner | e0bfdf1 | 2005-09-12 22:21:03 +0000 | [diff] [blame] | 8366 | // If the instruction immediately before this is a store to the same | 
|  | 8367 | // address, do a simple form of store->load forwarding. | 
| Chris Lattner | b990f7d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 8368 | if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) | 
|  | 8369 | if (SI->getOperand(1) == LI.getOperand(0)) | 
|  | 8370 | return ReplaceInstUsesWith(LI, SI->getOperand(0)); | 
| Chris Lattner | e0bfdf1 | 2005-09-12 22:21:03 +0000 | [diff] [blame] | 8371 | if (LoadInst *LIB = dyn_cast<LoadInst>(BBI)) | 
|  | 8372 | if (LIB->getOperand(0) == LI.getOperand(0)) | 
|  | 8373 | return ReplaceInstUsesWith(LI, LIB); | 
| Chris Lattner | b990f7d | 2005-09-12 22:00:15 +0000 | [diff] [blame] | 8374 | } | 
| Chris Lattner | a9d84e3 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 8375 |  | 
|  | 8376 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) | 
|  | 8377 | if (isa<ConstantPointerNull>(GEPI->getOperand(0)) || | 
|  | 8378 | isa<UndefValue>(GEPI->getOperand(0))) { | 
|  | 8379 | // Insert a new store to null instruction before the load to indicate | 
|  | 8380 | // that this code is not reachable.  We do this instead of inserting | 
|  | 8381 | // an unreachable instruction directly because we cannot modify the | 
|  | 8382 | // CFG. | 
|  | 8383 | new StoreInst(UndefValue::get(LI.getType()), | 
|  | 8384 | Constant::getNullValue(Op->getType()), &LI); | 
|  | 8385 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); | 
|  | 8386 | } | 
|  | 8387 |  | 
| Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8388 | if (Constant *C = dyn_cast<Constant>(Op)) { | 
| Chris Lattner | a9d84e3 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 8389 | // load null/undef -> undef | 
|  | 8390 | if ((C->isNullValue() || isa<UndefValue>(C))) { | 
| Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 8391 | // Insert a new store to null instruction before the load to indicate that | 
|  | 8392 | // this code is not reachable.  We do this instead of inserting an | 
|  | 8393 | // unreachable instruction directly because we cannot modify the CFG. | 
| Chris Lattner | a9d84e3 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 8394 | new StoreInst(UndefValue::get(LI.getType()), | 
|  | 8395 | Constant::getNullValue(Op->getType()), &LI); | 
| Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8396 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); | 
| Chris Lattner | 8ba9ec9 | 2004-10-18 02:59:09 +0000 | [diff] [blame] | 8397 | } | 
| Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 8398 |  | 
| Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8399 | // Instcombine load (constant global) into the value loaded. | 
|  | 8400 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Op)) | 
| Reid Spencer | 5301e7c | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 8401 | if (GV->isConstant() && !GV->isDeclaration()) | 
| Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8402 | return ReplaceInstUsesWith(LI, GV->getInitializer()); | 
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8403 |  | 
| Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8404 | // Instcombine load (constantexpr_GEP global, 0, ...) into the value loaded. | 
|  | 8405 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Op)) | 
|  | 8406 | if (CE->getOpcode() == Instruction::GetElementPtr) { | 
|  | 8407 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0))) | 
| Reid Spencer | 5301e7c | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 8408 | if (GV->isConstant() && !GV->isDeclaration()) | 
| Chris Lattner | 0b011ec | 2005-09-26 05:28:06 +0000 | [diff] [blame] | 8409 | if (Constant *V = | 
|  | 8410 | ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE)) | 
| Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8411 | return ReplaceInstUsesWith(LI, V); | 
| Chris Lattner | a9d84e3 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 8412 | if (CE->getOperand(0)->isNullValue()) { | 
|  | 8413 | // Insert a new store to null instruction before the load to indicate | 
|  | 8414 | // that this code is not reachable.  We do this instead of inserting | 
|  | 8415 | // an unreachable instruction directly because we cannot modify the | 
|  | 8416 | // CFG. | 
|  | 8417 | new StoreInst(UndefValue::get(LI.getType()), | 
|  | 8418 | Constant::getNullValue(Op->getType()), &LI); | 
|  | 8419 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); | 
|  | 8420 | } | 
|  | 8421 |  | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8422 | } else if (CE->isCast()) { | 
| Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8423 | if (Instruction *Res = InstCombineLoadCast(*this, LI)) | 
|  | 8424 | return Res; | 
|  | 8425 | } | 
|  | 8426 | } | 
| Chris Lattner | e228ee5 | 2004-04-08 20:39:49 +0000 | [diff] [blame] | 8427 |  | 
| Chris Lattner | a9d84e3 | 2005-05-01 04:24:53 +0000 | [diff] [blame] | 8428 | if (Op->hasOneUse()) { | 
| Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 8429 | // Change select and PHI nodes to select values instead of addresses: this | 
|  | 8430 | // helps alias analysis out a lot, allows many others simplifications, and | 
|  | 8431 | // exposes redundancy in the code. | 
|  | 8432 | // | 
|  | 8433 | // Note that we cannot do the transformation unless we know that the | 
|  | 8434 | // introduced loads cannot trap!  Something like this is valid as long as | 
|  | 8435 | // the condition is always false: load (select bool %C, int* null, int* %G), | 
|  | 8436 | // but it would not be valid if we transformed it to load from null | 
|  | 8437 | // unconditionally. | 
|  | 8438 | // | 
|  | 8439 | if (SelectInst *SI = dyn_cast<SelectInst>(Op)) { | 
|  | 8440 | // load (select (Cond, &V1, &V2))  --> select(Cond, load &V1, load &V2). | 
| Chris Lattner | e6f1309 | 2004-09-19 19:18:10 +0000 | [diff] [blame] | 8441 | if (isSafeToLoadUnconditionally(SI->getOperand(1), SI) && | 
|  | 8442 | isSafeToLoadUnconditionally(SI->getOperand(2), SI)) { | 
| Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 8443 | Value *V1 = InsertNewInstBefore(new LoadInst(SI->getOperand(1), | 
| Chris Lattner | 4261855 | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 8444 | SI->getOperand(1)->getName()+".val"), LI); | 
| Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 8445 | Value *V2 = InsertNewInstBefore(new LoadInst(SI->getOperand(2), | 
| Chris Lattner | 4261855 | 2004-09-20 10:15:10 +0000 | [diff] [blame] | 8446 | SI->getOperand(2)->getName()+".val"), LI); | 
| Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 8447 | return new SelectInst(SI->getCondition(), V1, V2); | 
|  | 8448 | } | 
|  | 8449 |  | 
| Chris Lattner | bdcf41a | 2004-09-23 15:46:00 +0000 | [diff] [blame] | 8450 | // load (select (cond, null, P)) -> load P | 
|  | 8451 | if (Constant *C = dyn_cast<Constant>(SI->getOperand(1))) | 
|  | 8452 | if (C->isNullValue()) { | 
|  | 8453 | LI.setOperand(0, SI->getOperand(2)); | 
|  | 8454 | return &LI; | 
|  | 8455 | } | 
|  | 8456 |  | 
|  | 8457 | // load (select (cond, P, null)) -> load P | 
|  | 8458 | if (Constant *C = dyn_cast<Constant>(SI->getOperand(2))) | 
|  | 8459 | if (C->isNullValue()) { | 
|  | 8460 | LI.setOperand(0, SI->getOperand(1)); | 
|  | 8461 | return &LI; | 
|  | 8462 | } | 
| Chris Lattner | f62ea8e | 2004-09-19 18:43:46 +0000 | [diff] [blame] | 8463 | } | 
|  | 8464 | } | 
| Chris Lattner | 0f1d8a3 | 2003-06-26 05:06:25 +0000 | [diff] [blame] | 8465 | return 0; | 
|  | 8466 | } | 
|  | 8467 |  | 
| Reid Spencer | e928a15 | 2007-01-19 21:20:31 +0000 | [diff] [blame] | 8468 | /// InstCombineStoreToCast - Fold store V, (cast P) -> store (cast V), P | 
| Chris Lattner | 72684fe | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8469 | /// when possible. | 
|  | 8470 | static Instruction *InstCombineStoreToCast(InstCombiner &IC, StoreInst &SI) { | 
|  | 8471 | User *CI = cast<User>(SI.getOperand(1)); | 
|  | 8472 | Value *CastOp = CI->getOperand(0); | 
|  | 8473 |  | 
|  | 8474 | const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType(); | 
|  | 8475 | if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) { | 
|  | 8476 | const Type *SrcPTy = SrcTy->getElementType(); | 
|  | 8477 |  | 
| Reid Spencer | 31a4ef4 | 2007-01-22 05:51:25 +0000 | [diff] [blame] | 8478 | if (DestPTy->isInteger() || isa<PointerType>(DestPTy)) { | 
| Chris Lattner | 72684fe | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8479 | // If the source is an array, the code below will not succeed.  Check to | 
|  | 8480 | // see if a trivial 'gep P, 0, 0' will help matters.  Only do this for | 
|  | 8481 | // constants. | 
|  | 8482 | if (const ArrayType *ASrcTy = dyn_cast<ArrayType>(SrcPTy)) | 
|  | 8483 | if (Constant *CSrc = dyn_cast<Constant>(CastOp)) | 
|  | 8484 | if (ASrcTy->getNumElements() != 0) { | 
| Chris Lattner | f96f4a8 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 8485 | Value* Idxs[2]; | 
|  | 8486 | Idxs[0] = Idxs[1] = Constant::getNullValue(Type::Int32Ty); | 
|  | 8487 | CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs, 2); | 
| Chris Lattner | 72684fe | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8488 | SrcTy = cast<PointerType>(CastOp->getType()); | 
|  | 8489 | SrcPTy = SrcTy->getElementType(); | 
|  | 8490 | } | 
|  | 8491 |  | 
| Reid Spencer | 9a4bed0 | 2007-01-20 23:35:48 +0000 | [diff] [blame] | 8492 | if ((SrcPTy->isInteger() || isa<PointerType>(SrcPTy)) && | 
|  | 8493 | IC.getTargetData().getTypeSizeInBits(SrcPTy) == | 
|  | 8494 | IC.getTargetData().getTypeSizeInBits(DestPTy)) { | 
| Chris Lattner | 72684fe | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8495 |  | 
|  | 8496 | // Okay, we are casting from one integer or pointer type to another of | 
| Reid Spencer | c050af9 | 2007-01-18 18:54:33 +0000 | [diff] [blame] | 8497 | // the same size.  Instead of casting the pointer before | 
|  | 8498 | // the store, cast the value to be stored. | 
| Chris Lattner | 72684fe | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8499 | Value *NewCast; | 
| Reid Spencer | bb65ebf | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 8500 | Value *SIOp0 = SI.getOperand(0); | 
| Reid Spencer | c050af9 | 2007-01-18 18:54:33 +0000 | [diff] [blame] | 8501 | Instruction::CastOps opcode = Instruction::BitCast; | 
|  | 8502 | const Type* CastSrcTy = SIOp0->getType(); | 
|  | 8503 | const Type* CastDstTy = SrcPTy; | 
|  | 8504 | if (isa<PointerType>(CastDstTy)) { | 
|  | 8505 | if (CastSrcTy->isInteger()) | 
| Reid Spencer | bb65ebf | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 8506 | opcode = Instruction::IntToPtr; | 
| Reid Spencer | 9a4bed0 | 2007-01-20 23:35:48 +0000 | [diff] [blame] | 8507 | } else if (isa<IntegerType>(CastDstTy)) { | 
| Reid Spencer | 74a528b | 2006-12-13 18:21:21 +0000 | [diff] [blame] | 8508 | if (isa<PointerType>(SIOp0->getType())) | 
| Reid Spencer | bb65ebf | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 8509 | opcode = Instruction::PtrToInt; | 
|  | 8510 | } | 
|  | 8511 | if (Constant *C = dyn_cast<Constant>(SIOp0)) | 
| Reid Spencer | c050af9 | 2007-01-18 18:54:33 +0000 | [diff] [blame] | 8512 | NewCast = ConstantExpr::getCast(opcode, C, CastDstTy); | 
| Chris Lattner | 72684fe | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8513 | else | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8514 | NewCast = IC.InsertNewInstBefore( | 
| Reid Spencer | c050af9 | 2007-01-18 18:54:33 +0000 | [diff] [blame] | 8515 | CastInst::create(opcode, SIOp0, CastDstTy, SIOp0->getName()+".c"), | 
|  | 8516 | SI); | 
| Chris Lattner | 72684fe | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8517 | return new StoreInst(NewCast, CastOp); | 
|  | 8518 | } | 
|  | 8519 | } | 
|  | 8520 | } | 
|  | 8521 | return 0; | 
|  | 8522 | } | 
|  | 8523 |  | 
| Chris Lattner | 31f486c | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 8524 | Instruction *InstCombiner::visitStoreInst(StoreInst &SI) { | 
|  | 8525 | Value *Val = SI.getOperand(0); | 
|  | 8526 | Value *Ptr = SI.getOperand(1); | 
|  | 8527 |  | 
|  | 8528 | if (isa<UndefValue>(Ptr)) {     // store X, undef -> noop (even if volatile) | 
| Chris Lattner | 5997cf9 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 8529 | EraseInstFromFunction(SI); | 
| Chris Lattner | 31f486c | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 8530 | ++NumCombined; | 
|  | 8531 | return 0; | 
|  | 8532 | } | 
| Chris Lattner | a4beeef | 2007-01-15 06:51:56 +0000 | [diff] [blame] | 8533 |  | 
|  | 8534 | // If the RHS is an alloca with a single use, zapify the store, making the | 
|  | 8535 | // alloca dead. | 
|  | 8536 | if (Ptr->hasOneUse()) { | 
|  | 8537 | if (isa<AllocaInst>(Ptr)) { | 
|  | 8538 | EraseInstFromFunction(SI); | 
|  | 8539 | ++NumCombined; | 
|  | 8540 | return 0; | 
|  | 8541 | } | 
|  | 8542 |  | 
|  | 8543 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) | 
|  | 8544 | if (isa<AllocaInst>(GEP->getOperand(0)) && | 
|  | 8545 | GEP->getOperand(0)->hasOneUse()) { | 
|  | 8546 | EraseInstFromFunction(SI); | 
|  | 8547 | ++NumCombined; | 
|  | 8548 | return 0; | 
|  | 8549 | } | 
|  | 8550 | } | 
| Chris Lattner | 31f486c | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 8551 |  | 
| Chris Lattner | 5997cf9 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 8552 | // Do really simple DSE, to catch cases where there are several consequtive | 
|  | 8553 | // stores to the same location, separated by a few arithmetic operations. This | 
|  | 8554 | // situation often occurs with bitfield accesses. | 
|  | 8555 | BasicBlock::iterator BBI = &SI; | 
|  | 8556 | for (unsigned ScanInsts = 6; BBI != SI.getParent()->begin() && ScanInsts; | 
|  | 8557 | --ScanInsts) { | 
|  | 8558 | --BBI; | 
|  | 8559 |  | 
|  | 8560 | if (StoreInst *PrevSI = dyn_cast<StoreInst>(BBI)) { | 
|  | 8561 | // Prev store isn't volatile, and stores to the same location? | 
|  | 8562 | if (!PrevSI->isVolatile() && PrevSI->getOperand(1) == SI.getOperand(1)) { | 
|  | 8563 | ++NumDeadStore; | 
|  | 8564 | ++BBI; | 
|  | 8565 | EraseInstFromFunction(*PrevSI); | 
|  | 8566 | continue; | 
|  | 8567 | } | 
|  | 8568 | break; | 
|  | 8569 | } | 
|  | 8570 |  | 
| Chris Lattner | dab43b2 | 2006-05-26 19:19:20 +0000 | [diff] [blame] | 8571 | // If this is a load, we have to stop.  However, if the loaded value is from | 
|  | 8572 | // the pointer we're loading and is producing the pointer we're storing, | 
|  | 8573 | // then *this* store is dead (X = load P; store X -> P). | 
|  | 8574 | if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) { | 
|  | 8575 | if (LI == Val && LI->getOperand(0) == Ptr) { | 
|  | 8576 | EraseInstFromFunction(SI); | 
|  | 8577 | ++NumCombined; | 
|  | 8578 | return 0; | 
|  | 8579 | } | 
|  | 8580 | // Otherwise, this is a load from some other location.  Stores before it | 
|  | 8581 | // may not be dead. | 
|  | 8582 | break; | 
|  | 8583 | } | 
|  | 8584 |  | 
| Chris Lattner | 5997cf9 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 8585 | // Don't skip over loads or things that can modify memory. | 
| Chris Lattner | dab43b2 | 2006-05-26 19:19:20 +0000 | [diff] [blame] | 8586 | if (BBI->mayWriteToMemory()) | 
| Chris Lattner | 5997cf9 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 8587 | break; | 
|  | 8588 | } | 
|  | 8589 |  | 
|  | 8590 |  | 
|  | 8591 | if (SI.isVolatile()) return 0;  // Don't hack volatile stores. | 
| Chris Lattner | 31f486c | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 8592 |  | 
|  | 8593 | // store X, null    -> turns into 'unreachable' in SimplifyCFG | 
|  | 8594 | if (isa<ConstantPointerNull>(Ptr)) { | 
|  | 8595 | if (!isa<UndefValue>(Val)) { | 
|  | 8596 | SI.setOperand(0, UndefValue::get(Val->getType())); | 
|  | 8597 | if (Instruction *U = dyn_cast<Instruction>(Val)) | 
| Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 8598 | AddToWorkList(U);  // Dropped a use. | 
| Chris Lattner | 31f486c | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 8599 | ++NumCombined; | 
|  | 8600 | } | 
|  | 8601 | return 0;  // Do not modify these! | 
|  | 8602 | } | 
|  | 8603 |  | 
|  | 8604 | // store undef, Ptr -> noop | 
|  | 8605 | if (isa<UndefValue>(Val)) { | 
| Chris Lattner | 5997cf9 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 8606 | EraseInstFromFunction(SI); | 
| Chris Lattner | 31f486c | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 8607 | ++NumCombined; | 
|  | 8608 | return 0; | 
|  | 8609 | } | 
|  | 8610 |  | 
| Chris Lattner | 72684fe | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8611 | // If the pointer destination is a cast, see if we can fold the cast into the | 
|  | 8612 | // source instead. | 
| Reid Spencer | de46e48 | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 8613 | if (isa<CastInst>(Ptr)) | 
| Chris Lattner | 72684fe | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8614 | if (Instruction *Res = InstCombineStoreToCast(*this, SI)) | 
|  | 8615 | return Res; | 
|  | 8616 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) | 
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 8617 | if (CE->isCast()) | 
| Chris Lattner | 72684fe | 2005-01-31 05:51:45 +0000 | [diff] [blame] | 8618 | if (Instruction *Res = InstCombineStoreToCast(*this, SI)) | 
|  | 8619 | return Res; | 
|  | 8620 |  | 
| Chris Lattner | 219175c | 2005-09-12 23:23:25 +0000 | [diff] [blame] | 8621 |  | 
|  | 8622 | // If this store is the last instruction in the basic block, and if the block | 
|  | 8623 | // ends with an unconditional branch, try to move it to the successor block. | 
| Chris Lattner | 5997cf9 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 8624 | BBI = &SI; ++BBI; | 
| Chris Lattner | 219175c | 2005-09-12 23:23:25 +0000 | [diff] [blame] | 8625 | if (BranchInst *BI = dyn_cast<BranchInst>(BBI)) | 
|  | 8626 | if (BI->isUnconditional()) { | 
|  | 8627 | // Check to see if the successor block has exactly two incoming edges.  If | 
|  | 8628 | // so, see if the other predecessor contains a store to the same location. | 
|  | 8629 | // if so, insert a PHI node (if needed) and move the stores down. | 
|  | 8630 | BasicBlock *Dest = BI->getSuccessor(0); | 
|  | 8631 |  | 
|  | 8632 | pred_iterator PI = pred_begin(Dest); | 
|  | 8633 | BasicBlock *Other = 0; | 
|  | 8634 | if (*PI != BI->getParent()) | 
|  | 8635 | Other = *PI; | 
|  | 8636 | ++PI; | 
|  | 8637 | if (PI != pred_end(Dest)) { | 
|  | 8638 | if (*PI != BI->getParent()) | 
|  | 8639 | if (Other) | 
|  | 8640 | Other = 0; | 
|  | 8641 | else | 
|  | 8642 | Other = *PI; | 
|  | 8643 | if (++PI != pred_end(Dest)) | 
|  | 8644 | Other = 0; | 
|  | 8645 | } | 
|  | 8646 | if (Other) {  // If only one other pred... | 
|  | 8647 | BBI = Other->getTerminator(); | 
|  | 8648 | // Make sure this other block ends in an unconditional branch and that | 
|  | 8649 | // there is an instruction before the branch. | 
|  | 8650 | if (isa<BranchInst>(BBI) && cast<BranchInst>(BBI)->isUnconditional() && | 
|  | 8651 | BBI != Other->begin()) { | 
|  | 8652 | --BBI; | 
|  | 8653 | StoreInst *OtherStore = dyn_cast<StoreInst>(BBI); | 
|  | 8654 |  | 
|  | 8655 | // If this instruction is a store to the same location. | 
|  | 8656 | if (OtherStore && OtherStore->getOperand(1) == SI.getOperand(1)) { | 
|  | 8657 | // Okay, we know we can perform this transformation.  Insert a PHI | 
|  | 8658 | // node now if we need it. | 
|  | 8659 | Value *MergedVal = OtherStore->getOperand(0); | 
|  | 8660 | if (MergedVal != SI.getOperand(0)) { | 
|  | 8661 | PHINode *PN = new PHINode(MergedVal->getType(), "storemerge"); | 
|  | 8662 | PN->reserveOperandSpace(2); | 
|  | 8663 | PN->addIncoming(SI.getOperand(0), SI.getParent()); | 
|  | 8664 | PN->addIncoming(OtherStore->getOperand(0), Other); | 
|  | 8665 | MergedVal = InsertNewInstBefore(PN, Dest->front()); | 
|  | 8666 | } | 
|  | 8667 |  | 
|  | 8668 | // Advance to a place where it is safe to insert the new store and | 
|  | 8669 | // insert it. | 
|  | 8670 | BBI = Dest->begin(); | 
|  | 8671 | while (isa<PHINode>(BBI)) ++BBI; | 
|  | 8672 | InsertNewInstBefore(new StoreInst(MergedVal, SI.getOperand(1), | 
|  | 8673 | OtherStore->isVolatile()), *BBI); | 
|  | 8674 |  | 
|  | 8675 | // Nuke the old stores. | 
| Chris Lattner | 5997cf9 | 2006-02-08 03:25:32 +0000 | [diff] [blame] | 8676 | EraseInstFromFunction(SI); | 
|  | 8677 | EraseInstFromFunction(*OtherStore); | 
| Chris Lattner | 219175c | 2005-09-12 23:23:25 +0000 | [diff] [blame] | 8678 | ++NumCombined; | 
|  | 8679 | return 0; | 
|  | 8680 | } | 
|  | 8681 | } | 
|  | 8682 | } | 
|  | 8683 | } | 
|  | 8684 |  | 
| Chris Lattner | 31f486c | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 8685 | return 0; | 
|  | 8686 | } | 
|  | 8687 |  | 
|  | 8688 |  | 
| Chris Lattner | 9eef8a7 | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 8689 | Instruction *InstCombiner::visitBranchInst(BranchInst &BI) { | 
|  | 8690 | // Change br (not X), label True, label False to: br X, label False, True | 
| Reid Spencer | 4fdd96c | 2005-06-18 17:37:34 +0000 | [diff] [blame] | 8691 | Value *X = 0; | 
| Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 8692 | BasicBlock *TrueDest; | 
|  | 8693 | BasicBlock *FalseDest; | 
|  | 8694 | if (match(&BI, m_Br(m_Not(m_Value(X)), TrueDest, FalseDest)) && | 
|  | 8695 | !isa<Constant>(X)) { | 
|  | 8696 | // Swap Destinations and condition... | 
|  | 8697 | BI.setCondition(X); | 
|  | 8698 | BI.setSuccessor(0, FalseDest); | 
|  | 8699 | BI.setSuccessor(1, TrueDest); | 
|  | 8700 | return &BI; | 
|  | 8701 | } | 
|  | 8702 |  | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8703 | // Cannonicalize fcmp_one -> fcmp_oeq | 
|  | 8704 | FCmpInst::Predicate FPred; Value *Y; | 
|  | 8705 | if (match(&BI, m_Br(m_FCmp(FPred, m_Value(X), m_Value(Y)), | 
|  | 8706 | TrueDest, FalseDest))) | 
|  | 8707 | if ((FPred == FCmpInst::FCMP_ONE || FPred == FCmpInst::FCMP_OLE || | 
|  | 8708 | FPred == FCmpInst::FCMP_OGE) && BI.getCondition()->hasOneUse()) { | 
|  | 8709 | FCmpInst *I = cast<FCmpInst>(BI.getCondition()); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8710 | FCmpInst::Predicate NewPred = FCmpInst::getInversePredicate(FPred); | 
| Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 8711 | Instruction *NewSCC = new FCmpInst(NewPred, X, Y, "", I); | 
|  | 8712 | NewSCC->takeName(I); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8713 | // Swap Destinations and condition... | 
|  | 8714 | BI.setCondition(NewSCC); | 
|  | 8715 | BI.setSuccessor(0, FalseDest); | 
|  | 8716 | BI.setSuccessor(1, TrueDest); | 
| Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 8717 | RemoveFromWorkList(I); | 
| Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 8718 | I->eraseFromParent(); | 
| Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 8719 | AddToWorkList(NewSCC); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8720 | return &BI; | 
|  | 8721 | } | 
|  | 8722 |  | 
|  | 8723 | // Cannonicalize icmp_ne -> icmp_eq | 
|  | 8724 | ICmpInst::Predicate IPred; | 
|  | 8725 | if (match(&BI, m_Br(m_ICmp(IPred, m_Value(X), m_Value(Y)), | 
|  | 8726 | TrueDest, FalseDest))) | 
|  | 8727 | if ((IPred == ICmpInst::ICMP_NE  || IPred == ICmpInst::ICMP_ULE || | 
|  | 8728 | IPred == ICmpInst::ICMP_SLE || IPred == ICmpInst::ICMP_UGE || | 
|  | 8729 | IPred == ICmpInst::ICMP_SGE) && BI.getCondition()->hasOneUse()) { | 
|  | 8730 | ICmpInst *I = cast<ICmpInst>(BI.getCondition()); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8731 | ICmpInst::Predicate NewPred = ICmpInst::getInversePredicate(IPred); | 
| Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 8732 | Instruction *NewSCC = new ICmpInst(NewPred, X, Y, "", I); | 
|  | 8733 | NewSCC->takeName(I); | 
| Chris Lattner | e967b34 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 8734 | // Swap Destinations and condition... | 
| Chris Lattner | d4252a7 | 2004-07-30 07:50:03 +0000 | [diff] [blame] | 8735 | BI.setCondition(NewSCC); | 
| Chris Lattner | e967b34 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 8736 | BI.setSuccessor(0, FalseDest); | 
|  | 8737 | BI.setSuccessor(1, TrueDest); | 
| Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 8738 | RemoveFromWorkList(I); | 
| Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 8739 | I->eraseFromParent();; | 
| Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 8740 | AddToWorkList(NewSCC); | 
| Chris Lattner | e967b34 | 2003-06-04 05:10:11 +0000 | [diff] [blame] | 8741 | return &BI; | 
|  | 8742 | } | 
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 8743 |  | 
| Chris Lattner | 9eef8a7 | 2003-06-04 04:46:00 +0000 | [diff] [blame] | 8744 | return 0; | 
|  | 8745 | } | 
| Chris Lattner | 1085bdf | 2002-11-04 16:18:53 +0000 | [diff] [blame] | 8746 |  | 
| Chris Lattner | 4c9c20a | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 8747 | Instruction *InstCombiner::visitSwitchInst(SwitchInst &SI) { | 
|  | 8748 | Value *Cond = SI.getCondition(); | 
|  | 8749 | if (Instruction *I = dyn_cast<Instruction>(Cond)) { | 
|  | 8750 | if (I->getOpcode() == Instruction::Add) | 
|  | 8751 | if (ConstantInt *AddRHS = dyn_cast<ConstantInt>(I->getOperand(1))) { | 
|  | 8752 | // change 'switch (X+4) case 1:' into 'switch (X) case -3' | 
|  | 8753 | for (unsigned i = 2, e = SI.getNumOperands(); i != e; i += 2) | 
| Chris Lattner | 81a7a23 | 2004-10-16 18:11:37 +0000 | [diff] [blame] | 8754 | SI.setOperand(i,ConstantExpr::getSub(cast<Constant>(SI.getOperand(i)), | 
| Chris Lattner | 4c9c20a | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 8755 | AddRHS)); | 
|  | 8756 | SI.setOperand(0, I->getOperand(0)); | 
| Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 8757 | AddToWorkList(I); | 
| Chris Lattner | 4c9c20a | 2004-07-03 00:26:11 +0000 | [diff] [blame] | 8758 | return &SI; | 
|  | 8759 | } | 
|  | 8760 | } | 
|  | 8761 | return 0; | 
|  | 8762 | } | 
|  | 8763 |  | 
| Chris Lattner | 6bc9865 | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 8764 | /// CheapToScalarize - Return true if the value is cheaper to scalarize than it | 
|  | 8765 | /// is to leave as a vector operation. | 
|  | 8766 | static bool CheapToScalarize(Value *V, bool isConstant) { | 
|  | 8767 | if (isa<ConstantAggregateZero>(V)) | 
|  | 8768 | return true; | 
| Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 8769 | if (ConstantVector *C = dyn_cast<ConstantVector>(V)) { | 
| Chris Lattner | 6bc9865 | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 8770 | if (isConstant) return true; | 
|  | 8771 | // If all elts are the same, we can extract. | 
|  | 8772 | Constant *Op0 = C->getOperand(0); | 
|  | 8773 | for (unsigned i = 1; i < C->getNumOperands(); ++i) | 
|  | 8774 | if (C->getOperand(i) != Op0) | 
|  | 8775 | return false; | 
|  | 8776 | return true; | 
|  | 8777 | } | 
|  | 8778 | Instruction *I = dyn_cast<Instruction>(V); | 
|  | 8779 | if (!I) return false; | 
|  | 8780 |  | 
|  | 8781 | // Insert element gets simplified to the inserted element or is deleted if | 
|  | 8782 | // this is constant idx extract element and its a constant idx insertelt. | 
|  | 8783 | if (I->getOpcode() == Instruction::InsertElement && isConstant && | 
|  | 8784 | isa<ConstantInt>(I->getOperand(2))) | 
|  | 8785 | return true; | 
|  | 8786 | if (I->getOpcode() == Instruction::Load && I->hasOneUse()) | 
|  | 8787 | return true; | 
|  | 8788 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) | 
|  | 8789 | if (BO->hasOneUse() && | 
|  | 8790 | (CheapToScalarize(BO->getOperand(0), isConstant) || | 
|  | 8791 | CheapToScalarize(BO->getOperand(1), isConstant))) | 
|  | 8792 | return true; | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 8793 | if (CmpInst *CI = dyn_cast<CmpInst>(I)) | 
|  | 8794 | if (CI->hasOneUse() && | 
|  | 8795 | (CheapToScalarize(CI->getOperand(0), isConstant) || | 
|  | 8796 | CheapToScalarize(CI->getOperand(1), isConstant))) | 
|  | 8797 | return true; | 
| Chris Lattner | 6bc9865 | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 8798 |  | 
|  | 8799 | return false; | 
|  | 8800 | } | 
|  | 8801 |  | 
| Chris Lattner | 945e437 | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 8802 | /// Read and decode a shufflevector mask. | 
|  | 8803 | /// | 
|  | 8804 | /// It turns undef elements into values that are larger than the number of | 
|  | 8805 | /// elements in the input. | 
| Chris Lattner | 12249be | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8806 | static std::vector<unsigned> getShuffleMask(const ShuffleVectorInst *SVI) { | 
|  | 8807 | unsigned NElts = SVI->getType()->getNumElements(); | 
|  | 8808 | if (isa<ConstantAggregateZero>(SVI->getOperand(2))) | 
|  | 8809 | return std::vector<unsigned>(NElts, 0); | 
|  | 8810 | if (isa<UndefValue>(SVI->getOperand(2))) | 
|  | 8811 | return std::vector<unsigned>(NElts, 2*NElts); | 
|  | 8812 |  | 
|  | 8813 | std::vector<unsigned> Result; | 
| Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 8814 | const ConstantVector *CP = cast<ConstantVector>(SVI->getOperand(2)); | 
| Chris Lattner | 12249be | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8815 | for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) | 
|  | 8816 | if (isa<UndefValue>(CP->getOperand(i))) | 
|  | 8817 | Result.push_back(NElts*2);  // undef -> 8 | 
|  | 8818 | else | 
| Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8819 | Result.push_back(cast<ConstantInt>(CP->getOperand(i))->getZExtValue()); | 
| Chris Lattner | 12249be | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8820 | return Result; | 
|  | 8821 | } | 
|  | 8822 |  | 
| Chris Lattner | 8d1d8d3 | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 8823 | /// FindScalarElement - Given a vector and an element number, see if the scalar | 
|  | 8824 | /// value is already around as a register, for example if it were inserted then | 
|  | 8825 | /// extracted from the vector. | 
|  | 8826 | static Value *FindScalarElement(Value *V, unsigned EltNo) { | 
| Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 8827 | assert(isa<VectorType>(V->getType()) && "Not looking at a vector?"); | 
|  | 8828 | const VectorType *PTy = cast<VectorType>(V->getType()); | 
| Chris Lattner | 2d37f92 | 2006-04-10 23:06:36 +0000 | [diff] [blame] | 8829 | unsigned Width = PTy->getNumElements(); | 
|  | 8830 | if (EltNo >= Width)  // Out of range access. | 
| Chris Lattner | 8d1d8d3 | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 8831 | return UndefValue::get(PTy->getElementType()); | 
|  | 8832 |  | 
|  | 8833 | if (isa<UndefValue>(V)) | 
|  | 8834 | return UndefValue::get(PTy->getElementType()); | 
|  | 8835 | else if (isa<ConstantAggregateZero>(V)) | 
|  | 8836 | return Constant::getNullValue(PTy->getElementType()); | 
| Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 8837 | else if (ConstantVector *CP = dyn_cast<ConstantVector>(V)) | 
| Chris Lattner | 8d1d8d3 | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 8838 | return CP->getOperand(EltNo); | 
|  | 8839 | else if (InsertElementInst *III = dyn_cast<InsertElementInst>(V)) { | 
|  | 8840 | // If this is an insert to a variable element, we don't know what it is. | 
| Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8841 | if (!isa<ConstantInt>(III->getOperand(2))) | 
|  | 8842 | return 0; | 
|  | 8843 | unsigned IIElt = cast<ConstantInt>(III->getOperand(2))->getZExtValue(); | 
| Chris Lattner | 8d1d8d3 | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 8844 |  | 
|  | 8845 | // If this is an insert to the element we are looking for, return the | 
|  | 8846 | // inserted value. | 
| Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8847 | if (EltNo == IIElt) | 
|  | 8848 | return III->getOperand(1); | 
| Chris Lattner | 8d1d8d3 | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 8849 |  | 
|  | 8850 | // Otherwise, the insertelement doesn't modify the value, recurse on its | 
|  | 8851 | // vector input. | 
|  | 8852 | return FindScalarElement(III->getOperand(0), EltNo); | 
| Chris Lattner | 2d37f92 | 2006-04-10 23:06:36 +0000 | [diff] [blame] | 8853 | } else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(V)) { | 
| Chris Lattner | 12249be | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8854 | unsigned InEl = getShuffleMask(SVI)[EltNo]; | 
|  | 8855 | if (InEl < Width) | 
|  | 8856 | return FindScalarElement(SVI->getOperand(0), InEl); | 
|  | 8857 | else if (InEl < Width*2) | 
|  | 8858 | return FindScalarElement(SVI->getOperand(1), InEl - Width); | 
|  | 8859 | else | 
|  | 8860 | return UndefValue::get(PTy->getElementType()); | 
| Chris Lattner | 8d1d8d3 | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 8861 | } | 
|  | 8862 |  | 
|  | 8863 | // Otherwise, we don't know. | 
|  | 8864 | return 0; | 
|  | 8865 | } | 
|  | 8866 |  | 
| Robert Bocchino | a835296 | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 8867 | Instruction *InstCombiner::visitExtractElementInst(ExtractElementInst &EI) { | 
| Chris Lattner | 8d1d8d3 | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 8868 |  | 
| Chris Lattner | 92346c3 | 2006-03-31 18:25:14 +0000 | [diff] [blame] | 8869 | // If packed val is undef, replace extract with scalar undef. | 
|  | 8870 | if (isa<UndefValue>(EI.getOperand(0))) | 
|  | 8871 | return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); | 
|  | 8872 |  | 
|  | 8873 | // If packed val is constant 0, replace extract with scalar 0. | 
|  | 8874 | if (isa<ConstantAggregateZero>(EI.getOperand(0))) | 
|  | 8875 | return ReplaceInstUsesWith(EI, Constant::getNullValue(EI.getType())); | 
|  | 8876 |  | 
| Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 8877 | if (ConstantVector *C = dyn_cast<ConstantVector>(EI.getOperand(0))) { | 
| Robert Bocchino | a835296 | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 8878 | // If packed val is constant with uniform operands, replace EI | 
|  | 8879 | // with that operand | 
| Chris Lattner | 6bc9865 | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 8880 | Constant *op0 = C->getOperand(0); | 
| Robert Bocchino | a835296 | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 8881 | for (unsigned i = 1; i < C->getNumOperands(); ++i) | 
| Chris Lattner | 6bc9865 | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 8882 | if (C->getOperand(i) != op0) { | 
|  | 8883 | op0 = 0; | 
|  | 8884 | break; | 
|  | 8885 | } | 
|  | 8886 | if (op0) | 
|  | 8887 | return ReplaceInstUsesWith(EI, op0); | 
| Robert Bocchino | a835296 | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 8888 | } | 
| Chris Lattner | 6bc9865 | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 8889 |  | 
| Chris Lattner | 8d1d8d3 | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 8890 | // If extracting a specified index from the vector, see if we can recursively | 
|  | 8891 | // find a previously computed scalar that was inserted into the vector. | 
| Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8892 | if (ConstantInt *IdxC = dyn_cast<ConstantInt>(EI.getOperand(1))) { | 
| Chris Lattner | 2deeaea | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 8893 | // This instruction only demands the single element from the input vector. | 
|  | 8894 | // If the input vector has a single use, simplify it based on this use | 
|  | 8895 | // property. | 
| Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8896 | uint64_t IndexVal = IdxC->getZExtValue(); | 
| Chris Lattner | 2deeaea | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 8897 | if (EI.getOperand(0)->hasOneUse()) { | 
|  | 8898 | uint64_t UndefElts; | 
|  | 8899 | if (Value *V = SimplifyDemandedVectorElts(EI.getOperand(0), | 
| Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8900 | 1 << IndexVal, | 
| Chris Lattner | 2deeaea | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 8901 | UndefElts)) { | 
|  | 8902 | EI.setOperand(0, V); | 
|  | 8903 | return &EI; | 
|  | 8904 | } | 
|  | 8905 | } | 
|  | 8906 |  | 
| Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8907 | if (Value *Elt = FindScalarElement(EI.getOperand(0), IndexVal)) | 
| Chris Lattner | 8d1d8d3 | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 8908 | return ReplaceInstUsesWith(EI, Elt); | 
| Chris Lattner | 2d37f92 | 2006-04-10 23:06:36 +0000 | [diff] [blame] | 8909 | } | 
| Chris Lattner | 8d1d8d3 | 2006-03-31 23:01:56 +0000 | [diff] [blame] | 8910 |  | 
| Chris Lattner | 83f6578 | 2006-05-25 22:53:38 +0000 | [diff] [blame] | 8911 | if (Instruction *I = dyn_cast<Instruction>(EI.getOperand(0))) { | 
| Robert Bocchino | a835296 | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 8912 | if (I->hasOneUse()) { | 
|  | 8913 | // Push extractelement into predecessor operation if legal and | 
|  | 8914 | // profitable to do so | 
|  | 8915 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) { | 
| Chris Lattner | 6bc9865 | 2006-03-05 00:22:33 +0000 | [diff] [blame] | 8916 | bool isConstantElt = isa<ConstantInt>(EI.getOperand(1)); | 
|  | 8917 | if (CheapToScalarize(BO, isConstantElt)) { | 
|  | 8918 | ExtractElementInst *newEI0 = | 
|  | 8919 | new ExtractElementInst(BO->getOperand(0), EI.getOperand(1), | 
|  | 8920 | EI.getName()+".lhs"); | 
|  | 8921 | ExtractElementInst *newEI1 = | 
|  | 8922 | new ExtractElementInst(BO->getOperand(1), EI.getOperand(1), | 
|  | 8923 | EI.getName()+".rhs"); | 
|  | 8924 | InsertNewInstBefore(newEI0, EI); | 
|  | 8925 | InsertNewInstBefore(newEI1, EI); | 
|  | 8926 | return BinaryOperator::create(BO->getOpcode(), newEI0, newEI1); | 
|  | 8927 | } | 
| Reid Spencer | de46e48 | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 8928 | } else if (isa<LoadInst>(I)) { | 
| Reid Spencer | 13bc5d7 | 2006-12-12 09:18:51 +0000 | [diff] [blame] | 8929 | Value *Ptr = InsertCastBefore(Instruction::BitCast, I->getOperand(0), | 
| Robert Bocchino | a835296 | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 8930 | PointerType::get(EI.getType()), EI); | 
|  | 8931 | GetElementPtrInst *GEP = | 
| Reid Spencer | a736fdf | 2006-11-29 01:11:01 +0000 | [diff] [blame] | 8932 | new GetElementPtrInst(Ptr, EI.getOperand(1), I->getName() + ".gep"); | 
| Robert Bocchino | a835296 | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 8933 | InsertNewInstBefore(GEP, EI); | 
|  | 8934 | return new LoadInst(GEP); | 
| Chris Lattner | 83f6578 | 2006-05-25 22:53:38 +0000 | [diff] [blame] | 8935 | } | 
|  | 8936 | } | 
|  | 8937 | if (InsertElementInst *IE = dyn_cast<InsertElementInst>(I)) { | 
|  | 8938 | // Extracting the inserted element? | 
|  | 8939 | if (IE->getOperand(2) == EI.getOperand(1)) | 
|  | 8940 | return ReplaceInstUsesWith(EI, IE->getOperand(1)); | 
|  | 8941 | // If the inserted and extracted elements are constants, they must not | 
|  | 8942 | // be the same value, extract from the pre-inserted value instead. | 
|  | 8943 | if (isa<Constant>(IE->getOperand(2)) && | 
|  | 8944 | isa<Constant>(EI.getOperand(1))) { | 
|  | 8945 | AddUsesToWorkList(EI); | 
|  | 8946 | EI.setOperand(0, IE->getOperand(0)); | 
|  | 8947 | return &EI; | 
|  | 8948 | } | 
|  | 8949 | } else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(I)) { | 
|  | 8950 | // If this is extracting an element from a shufflevector, figure out where | 
|  | 8951 | // it came from and extract from the appropriate input element instead. | 
| Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8952 | if (ConstantInt *Elt = dyn_cast<ConstantInt>(EI.getOperand(1))) { | 
|  | 8953 | unsigned SrcIdx = getShuffleMask(SVI)[Elt->getZExtValue()]; | 
| Chris Lattner | 12249be | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 8954 | Value *Src; | 
|  | 8955 | if (SrcIdx < SVI->getType()->getNumElements()) | 
|  | 8956 | Src = SVI->getOperand(0); | 
|  | 8957 | else if (SrcIdx < SVI->getType()->getNumElements()*2) { | 
|  | 8958 | SrcIdx -= SVI->getType()->getNumElements(); | 
|  | 8959 | Src = SVI->getOperand(1); | 
|  | 8960 | } else { | 
|  | 8961 | return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); | 
| Chris Lattner | 612fa8e | 2006-03-30 22:02:40 +0000 | [diff] [blame] | 8962 | } | 
| Chris Lattner | 2deeaea | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 8963 | return new ExtractElementInst(Src, SrcIdx); | 
| Robert Bocchino | a835296 | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 8964 | } | 
|  | 8965 | } | 
| Chris Lattner | 83f6578 | 2006-05-25 22:53:38 +0000 | [diff] [blame] | 8966 | } | 
| Robert Bocchino | a835296 | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 8967 | return 0; | 
|  | 8968 | } | 
|  | 8969 |  | 
| Chris Lattner | 9095186 | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8970 | /// CollectSingleShuffleElements - If V is a shuffle of values that ONLY returns | 
|  | 8971 | /// elements from either LHS or RHS, return the shuffle mask and true. | 
|  | 8972 | /// Otherwise, return false. | 
|  | 8973 | static bool CollectSingleShuffleElements(Value *V, Value *LHS, Value *RHS, | 
|  | 8974 | std::vector<Constant*> &Mask) { | 
|  | 8975 | assert(V->getType() == LHS->getType() && V->getType() == RHS->getType() && | 
|  | 8976 | "Invalid CollectSingleShuffleElements"); | 
| Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 8977 | unsigned NumElts = cast<VectorType>(V->getType())->getNumElements(); | 
| Chris Lattner | 9095186 | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8978 |  | 
|  | 8979 | if (isa<UndefValue>(V)) { | 
| Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8980 | Mask.assign(NumElts, UndefValue::get(Type::Int32Ty)); | 
| Chris Lattner | 9095186 | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8981 | return true; | 
|  | 8982 | } else if (V == LHS) { | 
|  | 8983 | for (unsigned i = 0; i != NumElts; ++i) | 
| Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8984 | Mask.push_back(ConstantInt::get(Type::Int32Ty, i)); | 
| Chris Lattner | 9095186 | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8985 | return true; | 
|  | 8986 | } else if (V == RHS) { | 
|  | 8987 | for (unsigned i = 0; i != NumElts; ++i) | 
| Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 8988 | Mask.push_back(ConstantInt::get(Type::Int32Ty, i+NumElts)); | 
| Chris Lattner | 9095186 | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 8989 | return true; | 
|  | 8990 | } else if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) { | 
|  | 8991 | // If this is an insert of an extract from some other vector, include it. | 
|  | 8992 | Value *VecOp    = IEI->getOperand(0); | 
|  | 8993 | Value *ScalarOp = IEI->getOperand(1); | 
|  | 8994 | Value *IdxOp    = IEI->getOperand(2); | 
|  | 8995 |  | 
| Chris Lattner | b6cb64b | 2006-04-27 21:14:21 +0000 | [diff] [blame] | 8996 | if (!isa<ConstantInt>(IdxOp)) | 
|  | 8997 | return false; | 
| Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 8998 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); | 
| Chris Lattner | b6cb64b | 2006-04-27 21:14:21 +0000 | [diff] [blame] | 8999 |  | 
|  | 9000 | if (isa<UndefValue>(ScalarOp)) {  // inserting undef into vector. | 
|  | 9001 | // Okay, we can handle this if the vector we are insertinting into is | 
|  | 9002 | // transitively ok. | 
|  | 9003 | if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) { | 
|  | 9004 | // If so, update the mask to reflect the inserted undef. | 
| Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9005 | Mask[InsertedIdx] = UndefValue::get(Type::Int32Ty); | 
| Chris Lattner | b6cb64b | 2006-04-27 21:14:21 +0000 | [diff] [blame] | 9006 | return true; | 
|  | 9007 | } | 
|  | 9008 | } else if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)){ | 
|  | 9009 | if (isa<ConstantInt>(EI->getOperand(1)) && | 
| Chris Lattner | 9095186 | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 9010 | EI->getOperand(0)->getType() == V->getType()) { | 
|  | 9011 | unsigned ExtractedIdx = | 
| Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 9012 | cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); | 
| Chris Lattner | 9095186 | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 9013 |  | 
|  | 9014 | // This must be extracting from either LHS or RHS. | 
|  | 9015 | if (EI->getOperand(0) == LHS || EI->getOperand(0) == RHS) { | 
|  | 9016 | // Okay, we can handle this if the vector we are insertinting into is | 
|  | 9017 | // transitively ok. | 
|  | 9018 | if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) { | 
|  | 9019 | // If so, update the mask to reflect the inserted value. | 
|  | 9020 | if (EI->getOperand(0) == LHS) { | 
|  | 9021 | Mask[InsertedIdx & (NumElts-1)] = | 
| Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9022 | ConstantInt::get(Type::Int32Ty, ExtractedIdx); | 
| Chris Lattner | 9095186 | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 9023 | } else { | 
|  | 9024 | assert(EI->getOperand(0) == RHS); | 
|  | 9025 | Mask[InsertedIdx & (NumElts-1)] = | 
| Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9026 | ConstantInt::get(Type::Int32Ty, ExtractedIdx+NumElts); | 
| Chris Lattner | 9095186 | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 9027 |  | 
|  | 9028 | } | 
|  | 9029 | return true; | 
|  | 9030 | } | 
|  | 9031 | } | 
|  | 9032 | } | 
|  | 9033 | } | 
|  | 9034 | } | 
|  | 9035 | // TODO: Handle shufflevector here! | 
|  | 9036 |  | 
|  | 9037 | return false; | 
|  | 9038 | } | 
|  | 9039 |  | 
|  | 9040 | /// CollectShuffleElements - We are building a shuffle of V, using RHS as the | 
|  | 9041 | /// RHS of the shuffle instruction, if it is not null.  Return a shuffle mask | 
|  | 9042 | /// that computes V and the LHS value of the shuffle. | 
| Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 9043 | static Value *CollectShuffleElements(Value *V, std::vector<Constant*> &Mask, | 
| Chris Lattner | 9095186 | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 9044 | Value *&RHS) { | 
| Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 9045 | assert(isa<VectorType>(V->getType()) && | 
| Chris Lattner | 9095186 | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 9046 | (RHS == 0 || V->getType() == RHS->getType()) && | 
| Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 9047 | "Invalid shuffle!"); | 
| Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 9048 | unsigned NumElts = cast<VectorType>(V->getType())->getNumElements(); | 
| Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 9049 |  | 
|  | 9050 | if (isa<UndefValue>(V)) { | 
| Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9051 | Mask.assign(NumElts, UndefValue::get(Type::Int32Ty)); | 
| Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 9052 | return V; | 
|  | 9053 | } else if (isa<ConstantAggregateZero>(V)) { | 
| Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9054 | Mask.assign(NumElts, ConstantInt::get(Type::Int32Ty, 0)); | 
| Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 9055 | return V; | 
|  | 9056 | } else if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) { | 
|  | 9057 | // If this is an insert of an extract from some other vector, include it. | 
|  | 9058 | Value *VecOp    = IEI->getOperand(0); | 
|  | 9059 | Value *ScalarOp = IEI->getOperand(1); | 
|  | 9060 | Value *IdxOp    = IEI->getOperand(2); | 
|  | 9061 |  | 
|  | 9062 | if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) { | 
|  | 9063 | if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) && | 
|  | 9064 | EI->getOperand(0)->getType() == V->getType()) { | 
|  | 9065 | unsigned ExtractedIdx = | 
| Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 9066 | cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); | 
|  | 9067 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); | 
| Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 9068 |  | 
|  | 9069 | // Either the extracted from or inserted into vector must be RHSVec, | 
|  | 9070 | // otherwise we'd end up with a shuffle of three inputs. | 
| Chris Lattner | 9095186 | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 9071 | if (EI->getOperand(0) == RHS || RHS == 0) { | 
|  | 9072 | RHS = EI->getOperand(0); | 
|  | 9073 | Value *V = CollectShuffleElements(VecOp, Mask, RHS); | 
| Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 9074 | Mask[InsertedIdx & (NumElts-1)] = | 
| Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9075 | ConstantInt::get(Type::Int32Ty, NumElts+ExtractedIdx); | 
| Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 9076 | return V; | 
|  | 9077 | } | 
|  | 9078 |  | 
| Chris Lattner | 9095186 | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 9079 | if (VecOp == RHS) { | 
|  | 9080 | Value *V = CollectShuffleElements(EI->getOperand(0), Mask, RHS); | 
| Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 9081 | // Everything but the extracted element is replaced with the RHS. | 
|  | 9082 | for (unsigned i = 0; i != NumElts; ++i) { | 
|  | 9083 | if (i != InsertedIdx) | 
| Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9084 | Mask[i] = ConstantInt::get(Type::Int32Ty, NumElts+i); | 
| Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 9085 | } | 
|  | 9086 | return V; | 
|  | 9087 | } | 
| Chris Lattner | 9095186 | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 9088 |  | 
|  | 9089 | // If this insertelement is a chain that comes from exactly these two | 
|  | 9090 | // vectors, return the vector and the effective shuffle. | 
|  | 9091 | if (CollectSingleShuffleElements(IEI, EI->getOperand(0), RHS, Mask)) | 
|  | 9092 | return EI->getOperand(0); | 
|  | 9093 |  | 
| Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 9094 | } | 
|  | 9095 | } | 
|  | 9096 | } | 
| Chris Lattner | 9095186 | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 9097 | // TODO: Handle shufflevector here! | 
| Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 9098 |  | 
|  | 9099 | // Otherwise, can't do anything fancy.  Return an identity vector. | 
|  | 9100 | for (unsigned i = 0; i != NumElts; ++i) | 
| Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9101 | Mask.push_back(ConstantInt::get(Type::Int32Ty, i)); | 
| Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 9102 | return V; | 
|  | 9103 | } | 
|  | 9104 |  | 
|  | 9105 | Instruction *InstCombiner::visitInsertElementInst(InsertElementInst &IE) { | 
|  | 9106 | Value *VecOp    = IE.getOperand(0); | 
|  | 9107 | Value *ScalarOp = IE.getOperand(1); | 
|  | 9108 | Value *IdxOp    = IE.getOperand(2); | 
|  | 9109 |  | 
|  | 9110 | // If the inserted element was extracted from some other vector, and if the | 
|  | 9111 | // indexes are constant, try to turn this into a shufflevector operation. | 
|  | 9112 | if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) { | 
|  | 9113 | if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) && | 
|  | 9114 | EI->getOperand(0)->getType() == IE.getType()) { | 
|  | 9115 | unsigned NumVectorElts = IE.getType()->getNumElements(); | 
| Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 9116 | unsigned ExtractedIdx=cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); | 
|  | 9117 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); | 
| Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 9118 |  | 
|  | 9119 | if (ExtractedIdx >= NumVectorElts) // Out of range extract. | 
|  | 9120 | return ReplaceInstUsesWith(IE, VecOp); | 
|  | 9121 |  | 
|  | 9122 | if (InsertedIdx >= NumVectorElts)  // Out of range insert. | 
|  | 9123 | return ReplaceInstUsesWith(IE, UndefValue::get(IE.getType())); | 
|  | 9124 |  | 
|  | 9125 | // If we are extracting a value from a vector, then inserting it right | 
|  | 9126 | // back into the same place, just use the input vector. | 
|  | 9127 | if (EI->getOperand(0) == VecOp && ExtractedIdx == InsertedIdx) | 
|  | 9128 | return ReplaceInstUsesWith(IE, VecOp); | 
|  | 9129 |  | 
|  | 9130 | // We could theoretically do this for ANY input.  However, doing so could | 
|  | 9131 | // turn chains of insertelement instructions into a chain of shufflevector | 
|  | 9132 | // instructions, and right now we do not merge shufflevectors.  As such, | 
|  | 9133 | // only do this in a situation where it is clear that there is benefit. | 
|  | 9134 | if (isa<UndefValue>(VecOp) || isa<ConstantAggregateZero>(VecOp)) { | 
|  | 9135 | // Turn this into shuffle(EIOp0, VecOp, Mask).  The result has all of | 
|  | 9136 | // the values of VecOp, except then one read from EIOp0. | 
|  | 9137 | // Build a new shuffle mask. | 
|  | 9138 | std::vector<Constant*> Mask; | 
|  | 9139 | if (isa<UndefValue>(VecOp)) | 
| Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9140 | Mask.assign(NumVectorElts, UndefValue::get(Type::Int32Ty)); | 
| Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 9141 | else { | 
|  | 9142 | assert(isa<ConstantAggregateZero>(VecOp) && "Unknown thing"); | 
| Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9143 | Mask.assign(NumVectorElts, ConstantInt::get(Type::Int32Ty, | 
| Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 9144 | NumVectorElts)); | 
|  | 9145 | } | 
| Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9146 | Mask[InsertedIdx] = ConstantInt::get(Type::Int32Ty, ExtractedIdx); | 
| Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 9147 | return new ShuffleVectorInst(EI->getOperand(0), VecOp, | 
| Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 9148 | ConstantVector::get(Mask)); | 
| Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 9149 | } | 
|  | 9150 |  | 
|  | 9151 | // If this insertelement isn't used by some other insertelement, turn it | 
|  | 9152 | // (and any insertelements it points to), into one big shuffle. | 
|  | 9153 | if (!IE.hasOneUse() || !isa<InsertElementInst>(IE.use_back())) { | 
|  | 9154 | std::vector<Constant*> Mask; | 
| Chris Lattner | 9095186 | 2006-04-16 00:51:47 +0000 | [diff] [blame] | 9155 | Value *RHS = 0; | 
|  | 9156 | Value *LHS = CollectShuffleElements(&IE, Mask, RHS); | 
|  | 9157 | if (RHS == 0) RHS = UndefValue::get(LHS->getType()); | 
|  | 9158 | // We now have a shuffle of LHS, RHS, Mask. | 
| Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 9159 | return new ShuffleVectorInst(LHS, RHS, ConstantVector::get(Mask)); | 
| Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 9160 | } | 
|  | 9161 | } | 
|  | 9162 | } | 
|  | 9163 |  | 
|  | 9164 | return 0; | 
|  | 9165 | } | 
|  | 9166 |  | 
|  | 9167 |  | 
| Chris Lattner | fbb77a4 | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 9168 | Instruction *InstCombiner::visitShuffleVectorInst(ShuffleVectorInst &SVI) { | 
|  | 9169 | Value *LHS = SVI.getOperand(0); | 
|  | 9170 | Value *RHS = SVI.getOperand(1); | 
| Chris Lattner | 12249be | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 9171 | std::vector<unsigned> Mask = getShuffleMask(&SVI); | 
| Chris Lattner | fbb77a4 | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 9172 |  | 
|  | 9173 | bool MadeChange = false; | 
|  | 9174 |  | 
| Chris Lattner | 2deeaea | 2006-10-05 06:55:50 +0000 | [diff] [blame] | 9175 | // Undefined shuffle mask -> undefined value. | 
| Chris Lattner | 12249be | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 9176 | if (isa<UndefValue>(SVI.getOperand(2))) | 
| Chris Lattner | fbb77a4 | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 9177 | return ReplaceInstUsesWith(SVI, UndefValue::get(SVI.getType())); | 
|  | 9178 |  | 
| Chris Lattner | d7b6ea1 | 2007-01-05 07:36:08 +0000 | [diff] [blame] | 9179 | // If we have shuffle(x, undef, mask) and any elements of mask refer to | 
| Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 9180 | // the undef, change them to undefs. | 
| Chris Lattner | d7b6ea1 | 2007-01-05 07:36:08 +0000 | [diff] [blame] | 9181 | if (isa<UndefValue>(SVI.getOperand(1))) { | 
|  | 9182 | // Scan to see if there are any references to the RHS.  If so, replace them | 
|  | 9183 | // with undef element refs and set MadeChange to true. | 
|  | 9184 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { | 
|  | 9185 | if (Mask[i] >= e && Mask[i] != 2*e) { | 
|  | 9186 | Mask[i] = 2*e; | 
|  | 9187 | MadeChange = true; | 
|  | 9188 | } | 
|  | 9189 | } | 
|  | 9190 |  | 
|  | 9191 | if (MadeChange) { | 
|  | 9192 | // Remap any references to RHS to use LHS. | 
|  | 9193 | std::vector<Constant*> Elts; | 
|  | 9194 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { | 
|  | 9195 | if (Mask[i] == 2*e) | 
|  | 9196 | Elts.push_back(UndefValue::get(Type::Int32Ty)); | 
|  | 9197 | else | 
|  | 9198 | Elts.push_back(ConstantInt::get(Type::Int32Ty, Mask[i])); | 
|  | 9199 | } | 
| Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 9200 | SVI.setOperand(2, ConstantVector::get(Elts)); | 
| Chris Lattner | d7b6ea1 | 2007-01-05 07:36:08 +0000 | [diff] [blame] | 9201 | } | 
|  | 9202 | } | 
| Chris Lattner | 39fac44 | 2006-04-15 01:39:45 +0000 | [diff] [blame] | 9203 |  | 
| Chris Lattner | 12249be | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 9204 | // Canonicalize shuffle(x    ,x,mask) -> shuffle(x, undef,mask') | 
|  | 9205 | // Canonicalize shuffle(undef,x,mask) -> shuffle(x, undef,mask'). | 
|  | 9206 | if (LHS == RHS || isa<UndefValue>(LHS)) { | 
|  | 9207 | if (isa<UndefValue>(LHS) && LHS == RHS) { | 
| Chris Lattner | fbb77a4 | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 9208 | // shuffle(undef,undef,mask) -> undef. | 
|  | 9209 | return ReplaceInstUsesWith(SVI, LHS); | 
|  | 9210 | } | 
|  | 9211 |  | 
| Chris Lattner | 12249be | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 9212 | // Remap any references to RHS to use LHS. | 
|  | 9213 | std::vector<Constant*> Elts; | 
|  | 9214 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { | 
| Chris Lattner | 0e47716 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 9215 | if (Mask[i] >= 2*e) | 
| Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9216 | Elts.push_back(UndefValue::get(Type::Int32Ty)); | 
| Chris Lattner | 0e47716 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 9217 | else { | 
|  | 9218 | if ((Mask[i] >= e && isa<UndefValue>(RHS)) || | 
|  | 9219 | (Mask[i] <  e && isa<UndefValue>(LHS))) | 
|  | 9220 | Mask[i] = 2*e;     // Turn into undef. | 
|  | 9221 | else | 
|  | 9222 | Mask[i] &= (e-1);  // Force to LHS. | 
| Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9223 | Elts.push_back(ConstantInt::get(Type::Int32Ty, Mask[i])); | 
| Chris Lattner | 0e47716 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 9224 | } | 
| Chris Lattner | fbb77a4 | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 9225 | } | 
| Chris Lattner | 12249be | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 9226 | SVI.setOperand(0, SVI.getOperand(1)); | 
| Chris Lattner | fbb77a4 | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 9227 | SVI.setOperand(1, UndefValue::get(RHS->getType())); | 
| Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 9228 | SVI.setOperand(2, ConstantVector::get(Elts)); | 
| Chris Lattner | 0e47716 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 9229 | LHS = SVI.getOperand(0); | 
|  | 9230 | RHS = SVI.getOperand(1); | 
| Chris Lattner | fbb77a4 | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 9231 | MadeChange = true; | 
|  | 9232 | } | 
|  | 9233 |  | 
| Chris Lattner | 0e47716 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 9234 | // Analyze the shuffle, are the LHS or RHS and identity shuffles? | 
| Chris Lattner | 12249be | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 9235 | bool isLHSID = true, isRHSID = true; | 
| Chris Lattner | 34cebe7 | 2006-04-16 00:03:56 +0000 | [diff] [blame] | 9236 |  | 
| Chris Lattner | 12249be | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 9237 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { | 
|  | 9238 | if (Mask[i] >= e*2) continue;  // Ignore undef values. | 
|  | 9239 | // Is this an identity shuffle of the LHS value? | 
|  | 9240 | isLHSID &= (Mask[i] == i); | 
|  | 9241 |  | 
|  | 9242 | // Is this an identity shuffle of the RHS value? | 
|  | 9243 | isRHSID &= (Mask[i]-e == i); | 
| Chris Lattner | 34cebe7 | 2006-04-16 00:03:56 +0000 | [diff] [blame] | 9244 | } | 
| Chris Lattner | fbb77a4 | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 9245 |  | 
| Chris Lattner | 12249be | 2006-05-25 23:48:38 +0000 | [diff] [blame] | 9246 | // Eliminate identity shuffles. | 
|  | 9247 | if (isLHSID) return ReplaceInstUsesWith(SVI, LHS); | 
|  | 9248 | if (isRHSID) return ReplaceInstUsesWith(SVI, RHS); | 
| Chris Lattner | fbb77a4 | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 9249 |  | 
| Chris Lattner | 0e47716 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 9250 | // If the LHS is a shufflevector itself, see if we can combine it with this | 
|  | 9251 | // one without producing an unusual shuffle.  Here we are really conservative: | 
|  | 9252 | // we are absolutely afraid of producing a shuffle mask not in the input | 
|  | 9253 | // program, because the code gen may not be smart enough to turn a merged | 
|  | 9254 | // shuffle into two specific shuffles: it may produce worse code.  As such, | 
|  | 9255 | // we only merge two shuffles if the result is one of the two input shuffle | 
|  | 9256 | // masks.  In this case, merging the shuffles just removes one instruction, | 
|  | 9257 | // which we know is safe.  This is good for things like turning: | 
|  | 9258 | // (splat(splat)) -> splat. | 
|  | 9259 | if (ShuffleVectorInst *LHSSVI = dyn_cast<ShuffleVectorInst>(LHS)) { | 
|  | 9260 | if (isa<UndefValue>(RHS)) { | 
|  | 9261 | std::vector<unsigned> LHSMask = getShuffleMask(LHSSVI); | 
|  | 9262 |  | 
|  | 9263 | std::vector<unsigned> NewMask; | 
|  | 9264 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) | 
|  | 9265 | if (Mask[i] >= 2*e) | 
|  | 9266 | NewMask.push_back(2*e); | 
|  | 9267 | else | 
|  | 9268 | NewMask.push_back(LHSMask[Mask[i]]); | 
|  | 9269 |  | 
|  | 9270 | // If the result mask is equal to the src shuffle or this shuffle mask, do | 
|  | 9271 | // the replacement. | 
|  | 9272 | if (NewMask == LHSMask || NewMask == Mask) { | 
|  | 9273 | std::vector<Constant*> Elts; | 
|  | 9274 | for (unsigned i = 0, e = NewMask.size(); i != e; ++i) { | 
|  | 9275 | if (NewMask[i] >= e*2) { | 
| Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9276 | Elts.push_back(UndefValue::get(Type::Int32Ty)); | 
| Chris Lattner | 0e47716 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 9277 | } else { | 
| Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 9278 | Elts.push_back(ConstantInt::get(Type::Int32Ty, NewMask[i])); | 
| Chris Lattner | 0e47716 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 9279 | } | 
|  | 9280 | } | 
|  | 9281 | return new ShuffleVectorInst(LHSSVI->getOperand(0), | 
|  | 9282 | LHSSVI->getOperand(1), | 
| Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 9283 | ConstantVector::get(Elts)); | 
| Chris Lattner | 0e47716 | 2006-05-26 00:29:06 +0000 | [diff] [blame] | 9284 | } | 
|  | 9285 | } | 
|  | 9286 | } | 
| Chris Lattner | 4284f64 | 2007-01-30 22:32:46 +0000 | [diff] [blame] | 9287 |  | 
| Chris Lattner | fbb77a4 | 2006-04-10 22:45:52 +0000 | [diff] [blame] | 9288 | return MadeChange ? &SVI : 0; | 
|  | 9289 | } | 
|  | 9290 |  | 
|  | 9291 |  | 
| Robert Bocchino | a835296 | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 9292 |  | 
| Chris Lattner | 39c98bb | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 9293 |  | 
|  | 9294 | /// TryToSinkInstruction - Try to move the specified instruction from its | 
|  | 9295 | /// current block into the beginning of DestBlock, which can only happen if it's | 
|  | 9296 | /// safe to move the instruction past all of the instructions between it and the | 
|  | 9297 | /// end of its block. | 
|  | 9298 | static bool TryToSinkInstruction(Instruction *I, BasicBlock *DestBlock) { | 
|  | 9299 | assert(I->hasOneUse() && "Invariants didn't hold!"); | 
|  | 9300 |  | 
| Chris Lattner | c4f67e6 | 2005-10-27 17:13:11 +0000 | [diff] [blame] | 9301 | // Cannot move control-flow-involving, volatile loads, vaarg, etc. | 
|  | 9302 | if (isa<PHINode>(I) || I->mayWriteToMemory()) return false; | 
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9303 |  | 
| Chris Lattner | 39c98bb | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 9304 | // Do not sink alloca instructions out of the entry block. | 
| Dan Gohman | dcb291f | 2007-03-22 16:38:57 +0000 | [diff] [blame] | 9305 | if (isa<AllocaInst>(I) && I->getParent() == | 
|  | 9306 | &DestBlock->getParent()->getEntryBlock()) | 
| Chris Lattner | 39c98bb | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 9307 | return false; | 
|  | 9308 |  | 
| Chris Lattner | f17a2fb | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 9309 | // We can only sink load instructions if there is nothing between the load and | 
|  | 9310 | // the end of block that could change the value. | 
|  | 9311 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) { | 
| Chris Lattner | f17a2fb | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 9312 | for (BasicBlock::iterator Scan = LI, E = LI->getParent()->end(); | 
|  | 9313 | Scan != E; ++Scan) | 
|  | 9314 | if (Scan->mayWriteToMemory()) | 
|  | 9315 | return false; | 
| Chris Lattner | f17a2fb | 2004-12-09 07:14:34 +0000 | [diff] [blame] | 9316 | } | 
| Chris Lattner | 39c98bb | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 9317 |  | 
|  | 9318 | BasicBlock::iterator InsertPos = DestBlock->begin(); | 
|  | 9319 | while (isa<PHINode>(InsertPos)) ++InsertPos; | 
|  | 9320 |  | 
| Chris Lattner | 9f269e4 | 2005-08-08 19:11:57 +0000 | [diff] [blame] | 9321 | I->moveBefore(InsertPos); | 
| Chris Lattner | 39c98bb | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 9322 | ++NumSunkInst; | 
|  | 9323 | return true; | 
|  | 9324 | } | 
|  | 9325 |  | 
| Chris Lattner | a36ee4e | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9326 |  | 
|  | 9327 | /// AddReachableCodeToWorklist - Walk the function in depth-first order, adding | 
|  | 9328 | /// all reachable code to the worklist. | 
|  | 9329 | /// | 
|  | 9330 | /// This has a couple of tricks to make the code faster and more powerful.  In | 
|  | 9331 | /// particular, we constant fold and DCE instructions as we go, to avoid adding | 
|  | 9332 | /// them to the worklist (this significantly speeds up instcombine on code where | 
|  | 9333 | /// many instructions are dead or constant).  Additionally, if we find a branch | 
|  | 9334 | /// whose condition is a known constant, we only visit the reachable successors. | 
|  | 9335 | /// | 
|  | 9336 | static void AddReachableCodeToWorklist(BasicBlock *BB, | 
| Chris Lattner | 7907e5f | 2007-02-15 19:41:52 +0000 | [diff] [blame] | 9337 | SmallPtrSet<BasicBlock*, 64> &Visited, | 
| Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9338 | InstCombiner &IC, | 
| Chris Lattner | 1443bc5 | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 9339 | const TargetData *TD) { | 
| Chris Lattner | 12b89cc | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 9340 | std::vector<BasicBlock*> Worklist; | 
|  | 9341 | Worklist.push_back(BB); | 
| Chris Lattner | a36ee4e | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9342 |  | 
| Chris Lattner | 12b89cc | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 9343 | while (!Worklist.empty()) { | 
|  | 9344 | BB = Worklist.back(); | 
|  | 9345 | Worklist.pop_back(); | 
|  | 9346 |  | 
|  | 9347 | // We have now visited this block!  If we've already been here, ignore it. | 
|  | 9348 | if (!Visited.insert(BB)) continue; | 
|  | 9349 |  | 
|  | 9350 | for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ) { | 
|  | 9351 | Instruction *Inst = BBI++; | 
| Chris Lattner | a36ee4e | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9352 |  | 
| Chris Lattner | 12b89cc | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 9353 | // DCE instruction if trivially dead. | 
|  | 9354 | if (isInstructionTriviallyDead(Inst)) { | 
|  | 9355 | ++NumDeadInst; | 
|  | 9356 | DOUT << "IC: DCE: " << *Inst; | 
|  | 9357 | Inst->eraseFromParent(); | 
|  | 9358 | continue; | 
|  | 9359 | } | 
|  | 9360 |  | 
|  | 9361 | // ConstantProp instruction if trivially constant. | 
|  | 9362 | if (Constant *C = ConstantFoldInstruction(Inst, TD)) { | 
|  | 9363 | DOUT << "IC: ConstFold to: " << *C << " from: " << *Inst; | 
|  | 9364 | Inst->replaceAllUsesWith(C); | 
|  | 9365 | ++NumConstProp; | 
|  | 9366 | Inst->eraseFromParent(); | 
|  | 9367 | continue; | 
|  | 9368 | } | 
|  | 9369 |  | 
|  | 9370 | IC.AddToWorkList(Inst); | 
| Chris Lattner | a36ee4e | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9371 | } | 
| Chris Lattner | 12b89cc | 2007-03-23 19:17:18 +0000 | [diff] [blame] | 9372 |  | 
|  | 9373 | // Recursively visit successors.  If this is a branch or switch on a | 
|  | 9374 | // constant, only visit the reachable successor. | 
|  | 9375 | TerminatorInst *TI = BB->getTerminator(); | 
|  | 9376 | if (BranchInst *BI = dyn_cast<BranchInst>(TI)) { | 
|  | 9377 | if (BI->isConditional() && isa<ConstantInt>(BI->getCondition())) { | 
|  | 9378 | bool CondVal = cast<ConstantInt>(BI->getCondition())->getZExtValue(); | 
|  | 9379 | Worklist.push_back(BI->getSuccessor(!CondVal)); | 
|  | 9380 | continue; | 
|  | 9381 | } | 
|  | 9382 | } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) { | 
|  | 9383 | if (ConstantInt *Cond = dyn_cast<ConstantInt>(SI->getCondition())) { | 
|  | 9384 | // See if this is an explicit destination. | 
|  | 9385 | for (unsigned i = 1, e = SI->getNumSuccessors(); i != e; ++i) | 
|  | 9386 | if (SI->getCaseValue(i) == Cond) { | 
|  | 9387 | Worklist.push_back(SI->getSuccessor(i)); | 
|  | 9388 | continue; | 
|  | 9389 | } | 
|  | 9390 |  | 
|  | 9391 | // Otherwise it is the default destination. | 
|  | 9392 | Worklist.push_back(SI->getSuccessor(0)); | 
|  | 9393 | continue; | 
|  | 9394 | } | 
|  | 9395 | } | 
|  | 9396 |  | 
|  | 9397 | for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) | 
|  | 9398 | Worklist.push_back(TI->getSuccessor(i)); | 
| Chris Lattner | a36ee4e | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9399 | } | 
| Chris Lattner | a36ee4e | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9400 | } | 
|  | 9401 |  | 
| Chris Lattner | 960a543 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 9402 | bool InstCombiner::DoOneIteration(Function &F, unsigned Iteration) { | 
| Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 9403 | bool Changed = false; | 
| Chris Lattner | f4ad165 | 2003-11-02 05:57:39 +0000 | [diff] [blame] | 9404 | TD = &getAnalysis<TargetData>(); | 
| Chris Lattner | 960a543 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 9405 |  | 
|  | 9406 | DEBUG(DOUT << "\n\nINSTCOMBINE ITERATION #" << Iteration << " on " | 
|  | 9407 | << F.getNameStr() << "\n"); | 
| Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 9408 |  | 
| Chris Lattner | 4ed40f7 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 9409 | { | 
| Chris Lattner | a36ee4e | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9410 | // Do a depth-first traversal of the function, populate the worklist with | 
|  | 9411 | // the reachable instructions.  Ignore blocks that are not reachable.  Keep | 
|  | 9412 | // track of which blocks we visit. | 
| Chris Lattner | 7907e5f | 2007-02-15 19:41:52 +0000 | [diff] [blame] | 9413 | SmallPtrSet<BasicBlock*, 64> Visited; | 
| Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9414 | AddReachableCodeToWorklist(F.begin(), Visited, *this, TD); | 
| Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 9415 |  | 
| Chris Lattner | 4ed40f7 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 9416 | // Do a quick scan over the function.  If we find any blocks that are | 
|  | 9417 | // unreachable, remove any instructions inside of them.  This prevents | 
|  | 9418 | // the instcombine code from having to deal with some bad special cases. | 
|  | 9419 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) | 
|  | 9420 | if (!Visited.count(BB)) { | 
|  | 9421 | Instruction *Term = BB->getTerminator(); | 
|  | 9422 | while (Term != BB->begin()) {   // Remove instrs bottom-up | 
|  | 9423 | BasicBlock::iterator I = Term; --I; | 
| Chris Lattner | 2d3a7a6 | 2004-04-27 15:13:33 +0000 | [diff] [blame] | 9424 |  | 
| Bill Wendling | 5dbf43c | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 9425 | DOUT << "IC: DCE: " << *I; | 
| Chris Lattner | 4ed40f7 | 2005-07-07 20:40:38 +0000 | [diff] [blame] | 9426 | ++NumDeadInst; | 
|  | 9427 |  | 
|  | 9428 | if (!I->use_empty()) | 
|  | 9429 | I->replaceAllUsesWith(UndefValue::get(I->getType())); | 
|  | 9430 | I->eraseFromParent(); | 
|  | 9431 | } | 
|  | 9432 | } | 
|  | 9433 | } | 
| Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 9434 |  | 
| Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9435 | while (!Worklist.empty()) { | 
|  | 9436 | Instruction *I = RemoveOneFromWorkList(); | 
|  | 9437 | if (I == 0) continue;  // skip null values. | 
| Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 9438 |  | 
| Chris Lattner | 1443bc5 | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 9439 | // Check to see if we can DCE the instruction. | 
| Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 9440 | if (isInstructionTriviallyDead(I)) { | 
| Chris Lattner | 1443bc5 | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 9441 | // Add operands to the worklist. | 
| Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 9442 | if (I->getNumOperands() < 4) | 
| Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 9443 | AddUsesToWorkList(*I); | 
| Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 9444 | ++NumDeadInst; | 
| Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 9445 |  | 
| Bill Wendling | 5dbf43c | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 9446 | DOUT << "IC: DCE: " << *I; | 
| Chris Lattner | cd517ff | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 9447 |  | 
|  | 9448 | I->eraseFromParent(); | 
| Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9449 | RemoveFromWorkList(I); | 
| Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 9450 | continue; | 
|  | 9451 | } | 
| Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 9452 |  | 
| Chris Lattner | 1443bc5 | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 9453 | // Instruction isn't dead, see if we can constant propagate it. | 
| Chris Lattner | e3eda25 | 2007-01-30 23:16:15 +0000 | [diff] [blame] | 9454 | if (Constant *C = ConstantFoldInstruction(I, TD)) { | 
| Bill Wendling | 5dbf43c | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 9455 | DOUT << "IC: ConstFold to: " << *C << " from: " << *I; | 
| Chris Lattner | cd517ff | 2005-01-28 19:32:01 +0000 | [diff] [blame] | 9456 |  | 
| Chris Lattner | 1443bc5 | 2006-05-11 17:11:52 +0000 | [diff] [blame] | 9457 | // Add operands to the worklist. | 
| Chris Lattner | 51ea127 | 2004-02-28 05:22:00 +0000 | [diff] [blame] | 9458 | AddUsesToWorkList(*I); | 
| Chris Lattner | c6509f4 | 2002-12-05 22:41:53 +0000 | [diff] [blame] | 9459 | ReplaceInstUsesWith(*I, C); | 
|  | 9460 |  | 
| Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 9461 | ++NumConstProp; | 
| Chris Lattner | a36ee4e | 2006-05-10 19:00:36 +0000 | [diff] [blame] | 9462 | I->eraseFromParent(); | 
| Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9463 | RemoveFromWorkList(I); | 
| Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 9464 | continue; | 
| Chris Lattner | 99f48c6 | 2002-09-02 04:59:56 +0000 | [diff] [blame] | 9465 | } | 
| Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 9466 |  | 
| Chris Lattner | 39c98bb | 2004-12-08 23:43:58 +0000 | [diff] [blame] | 9467 | // See if we can trivially sink this instruction to a successor basic block. | 
|  | 9468 | if (I->hasOneUse()) { | 
|  | 9469 | BasicBlock *BB = I->getParent(); | 
|  | 9470 | BasicBlock *UserParent = cast<Instruction>(I->use_back())->getParent(); | 
|  | 9471 | if (UserParent != BB) { | 
|  | 9472 | bool UserIsSuccessor = false; | 
|  | 9473 | // See if the user is one of our successors. | 
|  | 9474 | for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI) | 
|  | 9475 | if (*SI == UserParent) { | 
|  | 9476 | UserIsSuccessor = true; | 
|  | 9477 | break; | 
|  | 9478 | } | 
|  | 9479 |  | 
|  | 9480 | // If the user is one of our immediate successors, and if that successor | 
|  | 9481 | // only has us as a predecessors (we'd have to split the critical edge | 
|  | 9482 | // otherwise), we can keep going. | 
|  | 9483 | if (UserIsSuccessor && !isa<PHINode>(I->use_back()) && | 
|  | 9484 | next(pred_begin(UserParent)) == pred_end(UserParent)) | 
|  | 9485 | // Okay, the CFG is simple enough, try to sink this instruction. | 
|  | 9486 | Changed |= TryToSinkInstruction(I, UserParent); | 
|  | 9487 | } | 
|  | 9488 | } | 
|  | 9489 |  | 
| Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 9490 | // Now that we have an instruction, try combining it to simplify it... | 
| Reid Spencer | 755d0e7 | 2007-03-26 17:44:01 +0000 | [diff] [blame] | 9491 | #ifndef NDEBUG | 
|  | 9492 | std::string OrigI; | 
|  | 9493 | #endif | 
|  | 9494 | DEBUG(std::ostringstream SS; I->print(SS); OrigI = SS.str();); | 
| Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 9495 | if (Instruction *Result = visit(*I)) { | 
| Chris Lattner | 0b18c1d | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 9496 | ++NumCombined; | 
| Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 9497 | // Should we replace the old instruction with a new one? | 
| Chris Lattner | 053c093 | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 9498 | if (Result != I) { | 
| Bill Wendling | 5dbf43c | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 9499 | DOUT << "IC: Old = " << *I | 
|  | 9500 | << "    New = " << *Result; | 
| Chris Lattner | 7d2a539 | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 9501 |  | 
| Chris Lattner | 396dbfe | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 9502 | // Everything uses the new instruction now. | 
|  | 9503 | I->replaceAllUsesWith(Result); | 
|  | 9504 |  | 
|  | 9505 | // Push the new instruction and any users onto the worklist. | 
| Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9506 | AddToWorkList(Result); | 
| Chris Lattner | 396dbfe | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 9507 | AddUsersToWorkList(*Result); | 
| Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 9508 |  | 
| Chris Lattner | 6e0123b | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 9509 | // Move the name to the new instruction first. | 
|  | 9510 | Result->takeName(I); | 
| Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 9511 |  | 
|  | 9512 | // Insert the new instruction into the basic block... | 
|  | 9513 | BasicBlock *InstParent = I->getParent(); | 
| Chris Lattner | 7515cab | 2004-11-14 19:13:23 +0000 | [diff] [blame] | 9514 | BasicBlock::iterator InsertPos = I; | 
|  | 9515 |  | 
|  | 9516 | if (!isa<PHINode>(Result))        // If combining a PHI, don't insert | 
|  | 9517 | while (isa<PHINode>(InsertPos)) // middle of a block of PHIs. | 
|  | 9518 | ++InsertPos; | 
|  | 9519 |  | 
|  | 9520 | InstParent->getInstList().insert(InsertPos, Result); | 
| Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 9521 |  | 
| Chris Lattner | 63d75af | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 9522 | // Make sure that we reprocess all operands now that we reduced their | 
|  | 9523 | // use counts. | 
| Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9524 | AddUsesToWorkList(*I); | 
| Chris Lattner | b643a9e | 2004-05-01 23:19:52 +0000 | [diff] [blame] | 9525 |  | 
| Chris Lattner | 396dbfe | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 9526 | // Instructions can end up on the worklist more than once.  Make sure | 
|  | 9527 | // we do not process an instruction that has been deleted. | 
| Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9528 | RemoveFromWorkList(I); | 
| Chris Lattner | e8ed4ef | 2003-10-06 17:11:01 +0000 | [diff] [blame] | 9529 |  | 
|  | 9530 | // Erase the old instruction. | 
|  | 9531 | InstParent->getInstList().erase(I); | 
| Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 9532 | } else { | 
| Evan Cheng | a4ed8a5 | 2007-03-27 16:44:48 +0000 | [diff] [blame] | 9533 | #ifndef NDEBUG | 
| Reid Spencer | 755d0e7 | 2007-03-26 17:44:01 +0000 | [diff] [blame] | 9534 | DOUT << "IC: Mod = " << OrigI | 
|  | 9535 | << "    New = " << *I; | 
| Evan Cheng | a4ed8a5 | 2007-03-27 16:44:48 +0000 | [diff] [blame] | 9536 | #endif | 
| Chris Lattner | 7d2a539 | 2004-03-13 23:54:27 +0000 | [diff] [blame] | 9537 |  | 
| Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 9538 | // If the instruction was modified, it's possible that it is now dead. | 
|  | 9539 | // if so, remove it. | 
| Chris Lattner | 63d75af | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 9540 | if (isInstructionTriviallyDead(I)) { | 
|  | 9541 | // Make sure we process all operands now that we are reducing their | 
|  | 9542 | // use counts. | 
| Chris Lattner | 960a543 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 9543 | AddUsesToWorkList(*I); | 
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 9544 |  | 
| Chris Lattner | 63d75af | 2004-05-01 23:27:23 +0000 | [diff] [blame] | 9545 | // Instructions may end up in the worklist more than once.  Erase all | 
| Robert Bocchino | a835296 | 2006-01-13 22:48:06 +0000 | [diff] [blame] | 9546 | // occurrences of this instruction. | 
| Chris Lattner | b15e2b1 | 2007-03-02 21:28:56 +0000 | [diff] [blame] | 9547 | RemoveFromWorkList(I); | 
| Chris Lattner | 31f486c | 2005-01-31 05:36:43 +0000 | [diff] [blame] | 9548 | I->eraseFromParent(); | 
| Chris Lattner | 396dbfe | 2004-06-09 05:08:07 +0000 | [diff] [blame] | 9549 | } else { | 
| Chris Lattner | 960a543 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 9550 | AddToWorkList(I); | 
|  | 9551 | AddUsersToWorkList(*I); | 
| Chris Lattner | ae7a0d3 | 2002-08-02 19:29:35 +0000 | [diff] [blame] | 9552 | } | 
| Chris Lattner | 053c093 | 2002-05-14 15:24:07 +0000 | [diff] [blame] | 9553 | } | 
| Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 9554 | Changed = true; | 
| Chris Lattner | ca08125 | 2001-12-14 16:52:21 +0000 | [diff] [blame] | 9555 | } | 
|  | 9556 | } | 
|  | 9557 |  | 
| Chris Lattner | 960a543 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 9558 | assert(WorklistMap.empty() && "Worklist empty, but map not?"); | 
| Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 9559 | return Changed; | 
| Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 9560 | } | 
|  | 9561 |  | 
| Chris Lattner | 960a543 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 9562 |  | 
|  | 9563 | bool InstCombiner::runOnFunction(Function &F) { | 
| Chris Lattner | 8258b44 | 2007-03-04 04:27:24 +0000 | [diff] [blame] | 9564 | MustPreserveLCSSA = mustPreserveAnalysisID(LCSSAID); | 
|  | 9565 |  | 
| Chris Lattner | 960a543 | 2007-03-03 02:04:50 +0000 | [diff] [blame] | 9566 | bool EverMadeChange = false; | 
|  | 9567 |  | 
|  | 9568 | // Iterate while there is work to do. | 
|  | 9569 | unsigned Iteration = 0; | 
|  | 9570 | while (DoOneIteration(F, Iteration++)) | 
|  | 9571 | EverMadeChange = true; | 
|  | 9572 | return EverMadeChange; | 
|  | 9573 | } | 
|  | 9574 |  | 
| Brian Gaeke | 38b79e8 | 2004-07-27 17:43:21 +0000 | [diff] [blame] | 9575 | FunctionPass *llvm::createInstructionCombiningPass() { | 
| Chris Lattner | 260ab20 | 2002-04-18 17:39:14 +0000 | [diff] [blame] | 9576 | return new InstCombiner(); | 
| Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 9577 | } | 
| Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 9578 |  |