Chris Lattner | 9f3c25a | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 1 | //===- InstructionSimplify.cpp - Fold instruction operands ----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements routines for folding instructions into simpler forms |
| 11 | // that do not require creating new instructions. For example, this does |
| 12 | // constant folding, and can handle identities like (X&0)->0. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "llvm/Analysis/InstructionSimplify.h" |
| 17 | #include "llvm/Analysis/ConstantFolding.h" |
Chris Lattner | 40d8c28 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 18 | #include "llvm/Support/ValueHandle.h" |
Chris Lattner | 9f3c25a | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 19 | #include "llvm/Instructions.h" |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 20 | #include "llvm/Support/PatternMatch.h" |
Chris Lattner | 9f3c25a | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 21 | using namespace llvm; |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 22 | using namespace llvm::PatternMatch; |
Chris Lattner | 9f3c25a | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 23 | |
Duncan Sands | bc68d71 | 2010-11-10 20:53:24 +0000 | [diff] [blame] | 24 | #define MaxRecursionDepth 3 |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 25 | |
| 26 | static Value *SimplifyBinOp(unsigned, Value *, Value *, const TargetData *, |
| 27 | unsigned); |
| 28 | static Value *SimplifyCmpInst(unsigned, Value *, Value *, const TargetData *, |
| 29 | unsigned); |
| 30 | |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 31 | /// ThreadBinOpOverSelect - In the case of a binary operation with a select |
| 32 | /// instruction as an operand, try to simplify the binop by seeing whether |
| 33 | /// evaluating it on both branches of the select results in the same value. |
| 34 | /// Returns the common value if so, otherwise returns null. |
| 35 | static Value *ThreadBinOpOverSelect(unsigned Opcode, Value *LHS, Value *RHS, |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 36 | const TargetData *TD, unsigned MaxRecurse) { |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 37 | SelectInst *SI; |
| 38 | if (isa<SelectInst>(LHS)) { |
| 39 | SI = cast<SelectInst>(LHS); |
| 40 | } else { |
| 41 | assert(isa<SelectInst>(RHS) && "No select instruction operand!"); |
| 42 | SI = cast<SelectInst>(RHS); |
| 43 | } |
| 44 | |
| 45 | // Evaluate the BinOp on the true and false branches of the select. |
| 46 | Value *TV; |
| 47 | Value *FV; |
| 48 | if (SI == LHS) { |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 49 | TV = SimplifyBinOp(Opcode, SI->getTrueValue(), RHS, TD, MaxRecurse); |
| 50 | FV = SimplifyBinOp(Opcode, SI->getFalseValue(), RHS, TD, MaxRecurse); |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 51 | } else { |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 52 | TV = SimplifyBinOp(Opcode, LHS, SI->getTrueValue(), TD, MaxRecurse); |
| 53 | FV = SimplifyBinOp(Opcode, LHS, SI->getFalseValue(), TD, MaxRecurse); |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | // If they simplified to the same value, then return the common value. |
| 57 | // If they both failed to simplify then return null. |
| 58 | if (TV == FV) |
| 59 | return TV; |
| 60 | |
| 61 | // If one branch simplified to undef, return the other one. |
| 62 | if (TV && isa<UndefValue>(TV)) |
| 63 | return FV; |
| 64 | if (FV && isa<UndefValue>(FV)) |
| 65 | return TV; |
| 66 | |
| 67 | // If applying the operation did not change the true and false select values, |
| 68 | // then the result of the binop is the select itself. |
| 69 | if (TV == SI->getTrueValue() && FV == SI->getFalseValue()) |
| 70 | return SI; |
| 71 | |
| 72 | // If one branch simplified and the other did not, and the simplified |
| 73 | // value is equal to the unsimplified one, return the simplified value. |
| 74 | // For example, select (cond, X, X & Z) & Z -> X & Z. |
| 75 | if ((FV && !TV) || (TV && !FV)) { |
| 76 | // Check that the simplified value has the form "X op Y" where "op" is the |
| 77 | // same as the original operation. |
| 78 | Instruction *Simplified = dyn_cast<Instruction>(FV ? FV : TV); |
| 79 | if (Simplified && Simplified->getOpcode() == Opcode) { |
| 80 | // The value that didn't simplify is "UnsimplifiedLHS op UnsimplifiedRHS". |
| 81 | // We already know that "op" is the same as for the simplified value. See |
| 82 | // if the operands match too. If so, return the simplified value. |
| 83 | Value *UnsimplifiedBranch = FV ? SI->getTrueValue() : SI->getFalseValue(); |
| 84 | Value *UnsimplifiedLHS = SI == LHS ? UnsimplifiedBranch : LHS; |
| 85 | Value *UnsimplifiedRHS = SI == LHS ? RHS : UnsimplifiedBranch; |
| 86 | if (Simplified->getOperand(0) == UnsimplifiedLHS && |
| 87 | Simplified->getOperand(1) == UnsimplifiedRHS) |
| 88 | return Simplified; |
| 89 | if (Simplified->isCommutative() && |
| 90 | Simplified->getOperand(1) == UnsimplifiedLHS && |
| 91 | Simplified->getOperand(0) == UnsimplifiedRHS) |
| 92 | return Simplified; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | /// ThreadCmpOverSelect - In the case of a comparison with a select instruction, |
| 100 | /// try to simplify the comparison by seeing whether both branches of the select |
| 101 | /// result in the same value. Returns the common value if so, otherwise returns |
| 102 | /// null. |
| 103 | static Value *ThreadCmpOverSelect(CmpInst::Predicate Pred, Value *LHS, |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 104 | Value *RHS, const TargetData *TD, |
| 105 | unsigned MaxRecurse) { |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 106 | // Make sure the select is on the LHS. |
| 107 | if (!isa<SelectInst>(LHS)) { |
| 108 | std::swap(LHS, RHS); |
| 109 | Pred = CmpInst::getSwappedPredicate(Pred); |
| 110 | } |
| 111 | assert(isa<SelectInst>(LHS) && "Not comparing with a select instruction!"); |
| 112 | SelectInst *SI = cast<SelectInst>(LHS); |
| 113 | |
| 114 | // Now that we have "cmp select(cond, TV, FV), RHS", analyse it. |
| 115 | // Does "cmp TV, RHS" simplify? |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 116 | if (Value *TCmp = SimplifyCmpInst(Pred, SI->getTrueValue(), RHS, TD, |
| 117 | MaxRecurse)) |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 118 | // It does! Does "cmp FV, RHS" simplify? |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 119 | if (Value *FCmp = SimplifyCmpInst(Pred, SI->getFalseValue(), RHS, TD, |
| 120 | MaxRecurse)) |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 121 | // It does! If they simplified to the same value, then use it as the |
| 122 | // result of the original comparison. |
| 123 | if (TCmp == FCmp) |
| 124 | return TCmp; |
| 125 | return 0; |
| 126 | } |
| 127 | |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 128 | /// ThreadBinOpOverPHI - In the case of a binary operation with an operand that |
| 129 | /// is a PHI instruction, try to simplify the binop by seeing whether evaluating |
| 130 | /// it on the incoming phi values yields the same result for every value. If so |
| 131 | /// returns the common value, otherwise returns null. |
| 132 | static Value *ThreadBinOpOverPHI(unsigned Opcode, Value *LHS, Value *RHS, |
| 133 | const TargetData *TD, unsigned MaxRecurse) { |
| 134 | PHINode *PI; |
| 135 | if (isa<PHINode>(LHS)) { |
| 136 | PI = cast<PHINode>(LHS); |
| 137 | } else { |
| 138 | assert(isa<PHINode>(RHS) && "No PHI instruction operand!"); |
| 139 | PI = cast<PHINode>(RHS); |
| 140 | } |
| 141 | |
| 142 | // Evaluate the BinOp on the incoming phi values. |
| 143 | Value *CommonValue = 0; |
| 144 | for (unsigned i = 0, e = PI->getNumIncomingValues(); i != e; ++i) { |
Duncan Sands | 5520089 | 2010-11-15 17:52:45 +0000 | [diff] [blame] | 145 | Value *Incoming = PI->getIncomingValue(i); |
| 146 | // If the incoming value is the phi node itself, it can be safely skipped. |
| 147 | if (Incoming == PI) continue; |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 148 | Value *V = PI == LHS ? |
Duncan Sands | 5520089 | 2010-11-15 17:52:45 +0000 | [diff] [blame] | 149 | SimplifyBinOp(Opcode, Incoming, RHS, TD, MaxRecurse) : |
| 150 | SimplifyBinOp(Opcode, LHS, Incoming, TD, MaxRecurse); |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 151 | // If the operation failed to simplify, or simplified to a different value |
| 152 | // to previously, then give up. |
| 153 | if (!V || (CommonValue && V != CommonValue)) |
| 154 | return 0; |
| 155 | CommonValue = V; |
| 156 | } |
| 157 | |
| 158 | return CommonValue; |
| 159 | } |
| 160 | |
| 161 | /// ThreadCmpOverPHI - In the case of a comparison with a PHI instruction, try |
| 162 | /// try to simplify the comparison by seeing whether comparing with all of the |
| 163 | /// incoming phi values yields the same result every time. If so returns the |
| 164 | /// common result, otherwise returns null. |
| 165 | static Value *ThreadCmpOverPHI(CmpInst::Predicate Pred, Value *LHS, Value *RHS, |
| 166 | const TargetData *TD, unsigned MaxRecurse) { |
| 167 | // Make sure the phi is on the LHS. |
| 168 | if (!isa<PHINode>(LHS)) { |
| 169 | std::swap(LHS, RHS); |
| 170 | Pred = CmpInst::getSwappedPredicate(Pred); |
| 171 | } |
| 172 | assert(isa<PHINode>(LHS) && "Not comparing with a phi instruction!"); |
| 173 | PHINode *PI = cast<PHINode>(LHS); |
| 174 | |
| 175 | // Evaluate the BinOp on the incoming phi values. |
| 176 | Value *CommonValue = 0; |
| 177 | for (unsigned i = 0, e = PI->getNumIncomingValues(); i != e; ++i) { |
Duncan Sands | 5520089 | 2010-11-15 17:52:45 +0000 | [diff] [blame] | 178 | Value *Incoming = PI->getIncomingValue(i); |
| 179 | // If the incoming value is the phi node itself, it can be safely skipped. |
| 180 | if (Incoming == PI) continue; |
| 181 | Value *V = SimplifyCmpInst(Pred, Incoming, RHS, TD, MaxRecurse); |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 182 | // If the operation failed to simplify, or simplified to a different value |
| 183 | // to previously, then give up. |
| 184 | if (!V || (CommonValue && V != CommonValue)) |
| 185 | return 0; |
| 186 | CommonValue = V; |
| 187 | } |
| 188 | |
| 189 | return CommonValue; |
| 190 | } |
| 191 | |
Chris Lattner | 8aee8ef | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 192 | /// SimplifyAddInst - Given operands for an Add, see if we can |
| 193 | /// fold the result. If not, this returns null. |
| 194 | Value *llvm::SimplifyAddInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
| 195 | const TargetData *TD) { |
| 196 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
| 197 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) { |
| 198 | Constant *Ops[] = { CLHS, CRHS }; |
| 199 | return ConstantFoldInstOperands(Instruction::Add, CLHS->getType(), |
| 200 | Ops, 2, TD); |
| 201 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 202 | |
Chris Lattner | 8aee8ef | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 203 | // Canonicalize the constant to the RHS. |
| 204 | std::swap(Op0, Op1); |
| 205 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 206 | |
Chris Lattner | 8aee8ef | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 207 | if (Constant *Op1C = dyn_cast<Constant>(Op1)) { |
| 208 | // X + undef -> undef |
| 209 | if (isa<UndefValue>(Op1C)) |
| 210 | return Op1C; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 211 | |
Chris Lattner | 8aee8ef | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 212 | // X + 0 --> X |
| 213 | if (Op1C->isNullValue()) |
| 214 | return Op0; |
| 215 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 216 | |
Chris Lattner | 8aee8ef | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 217 | // FIXME: Could pull several more out of instcombine. |
| 218 | return 0; |
| 219 | } |
| 220 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 221 | /// SimplifyAndInst - Given operands for an And, see if we can |
Chris Lattner | 9f3c25a | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 222 | /// fold the result. If not, this returns null. |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 223 | static Value *SimplifyAndInst(Value *Op0, Value *Op1, const TargetData *TD, |
| 224 | unsigned MaxRecurse) { |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 225 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
| 226 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) { |
| 227 | Constant *Ops[] = { CLHS, CRHS }; |
| 228 | return ConstantFoldInstOperands(Instruction::And, CLHS->getType(), |
| 229 | Ops, 2, TD); |
| 230 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 231 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 232 | // Canonicalize the constant to the RHS. |
| 233 | std::swap(Op0, Op1); |
| 234 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 235 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 236 | // X & undef -> 0 |
| 237 | if (isa<UndefValue>(Op1)) |
| 238 | return Constant::getNullValue(Op0->getType()); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 239 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 240 | // X & X = X |
| 241 | if (Op0 == Op1) |
| 242 | return Op0; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 243 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 244 | // X & <0,0> = <0,0> |
| 245 | if (isa<ConstantAggregateZero>(Op1)) |
| 246 | return Op1; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 247 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 248 | // X & <-1,-1> = X |
| 249 | if (ConstantVector *CP = dyn_cast<ConstantVector>(Op1)) |
| 250 | if (CP->isAllOnesValue()) |
| 251 | return Op0; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 252 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 253 | if (ConstantInt *Op1CI = dyn_cast<ConstantInt>(Op1)) { |
| 254 | // X & 0 = 0 |
| 255 | if (Op1CI->isZero()) |
| 256 | return Op1CI; |
| 257 | // X & -1 = X |
| 258 | if (Op1CI->isAllOnesValue()) |
| 259 | return Op0; |
| 260 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 261 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 262 | // A & ~A = ~A & A = 0 |
| 263 | Value *A, *B; |
Chris Lattner | 70ce6d0 | 2009-11-10 02:04:54 +0000 | [diff] [blame] | 264 | if ((match(Op0, m_Not(m_Value(A))) && A == Op1) || |
| 265 | (match(Op1, m_Not(m_Value(A))) && A == Op0)) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 266 | return Constant::getNullValue(Op0->getType()); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 267 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 268 | // (A | ?) & A = A |
| 269 | if (match(Op0, m_Or(m_Value(A), m_Value(B))) && |
| 270 | (A == Op1 || B == Op1)) |
| 271 | return Op1; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 272 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 273 | // A & (A | ?) = A |
| 274 | if (match(Op1, m_Or(m_Value(A), m_Value(B))) && |
| 275 | (A == Op0 || B == Op0)) |
| 276 | return Op0; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 277 | |
Benjamin Kramer | 6844c8e | 2010-09-10 22:39:55 +0000 | [diff] [blame] | 278 | // (A & B) & A -> A & B |
| 279 | if (match(Op0, m_And(m_Value(A), m_Value(B))) && |
| 280 | (A == Op1 || B == Op1)) |
| 281 | return Op0; |
| 282 | |
| 283 | // A & (A & B) -> A & B |
| 284 | if (match(Op1, m_And(m_Value(A), m_Value(B))) && |
| 285 | (A == Op0 || B == Op0)) |
| 286 | return Op1; |
| 287 | |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 288 | // If the operation is with the result of a select instruction, check whether |
| 289 | // operating on either branch of the select always yields the same value. |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 290 | if (MaxRecurse && (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))) |
| 291 | if (Value *V = ThreadBinOpOverSelect(Instruction::And, Op0, Op1, TD, |
| 292 | MaxRecurse-1)) |
| 293 | return V; |
| 294 | |
| 295 | // If the operation is with the result of a phi instruction, check whether |
| 296 | // operating on all incoming values of the phi always yields the same value. |
| 297 | if (MaxRecurse && (isa<PHINode>(Op0) || isa<PHINode>(Op1))) |
| 298 | if (Value *V = ThreadBinOpOverPHI(Instruction::And, Op0, Op1, TD, |
| 299 | MaxRecurse-1)) |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 300 | return V; |
| 301 | |
Chris Lattner | 9f3c25a | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 302 | return 0; |
| 303 | } |
| 304 | |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 305 | Value *llvm::SimplifyAndInst(Value *Op0, Value *Op1, const TargetData *TD) { |
| 306 | return ::SimplifyAndInst(Op0, Op1, TD, MaxRecursionDepth); |
| 307 | } |
| 308 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 309 | /// SimplifyOrInst - Given operands for an Or, see if we can |
| 310 | /// fold the result. If not, this returns null. |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 311 | static Value *SimplifyOrInst(Value *Op0, Value *Op1, const TargetData *TD, |
| 312 | unsigned MaxRecurse) { |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 313 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
| 314 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) { |
| 315 | Constant *Ops[] = { CLHS, CRHS }; |
| 316 | return ConstantFoldInstOperands(Instruction::Or, CLHS->getType(), |
| 317 | Ops, 2, TD); |
| 318 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 319 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 320 | // Canonicalize the constant to the RHS. |
| 321 | std::swap(Op0, Op1); |
| 322 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 323 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 324 | // X | undef -> -1 |
| 325 | if (isa<UndefValue>(Op1)) |
| 326 | return Constant::getAllOnesValue(Op0->getType()); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 327 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 328 | // X | X = X |
| 329 | if (Op0 == Op1) |
| 330 | return Op0; |
| 331 | |
| 332 | // X | <0,0> = X |
| 333 | if (isa<ConstantAggregateZero>(Op1)) |
| 334 | return Op0; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 335 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 336 | // X | <-1,-1> = <-1,-1> |
| 337 | if (ConstantVector *CP = dyn_cast<ConstantVector>(Op1)) |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 338 | if (CP->isAllOnesValue()) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 339 | return Op1; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 340 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 341 | if (ConstantInt *Op1CI = dyn_cast<ConstantInt>(Op1)) { |
| 342 | // X | 0 = X |
| 343 | if (Op1CI->isZero()) |
| 344 | return Op0; |
| 345 | // X | -1 = -1 |
| 346 | if (Op1CI->isAllOnesValue()) |
| 347 | return Op1CI; |
| 348 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 349 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 350 | // A | ~A = ~A | A = -1 |
| 351 | Value *A, *B; |
Chris Lattner | 70ce6d0 | 2009-11-10 02:04:54 +0000 | [diff] [blame] | 352 | if ((match(Op0, m_Not(m_Value(A))) && A == Op1) || |
| 353 | (match(Op1, m_Not(m_Value(A))) && A == Op0)) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 354 | return Constant::getAllOnesValue(Op0->getType()); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 355 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 356 | // (A & ?) | A = A |
| 357 | if (match(Op0, m_And(m_Value(A), m_Value(B))) && |
| 358 | (A == Op1 || B == Op1)) |
| 359 | return Op1; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 360 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 361 | // A | (A & ?) = A |
| 362 | if (match(Op1, m_And(m_Value(A), m_Value(B))) && |
| 363 | (A == Op0 || B == Op0)) |
| 364 | return Op0; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 365 | |
Benjamin Kramer | 6844c8e | 2010-09-10 22:39:55 +0000 | [diff] [blame] | 366 | // (A | B) | A -> A | B |
| 367 | if (match(Op0, m_Or(m_Value(A), m_Value(B))) && |
| 368 | (A == Op1 || B == Op1)) |
| 369 | return Op0; |
| 370 | |
| 371 | // A | (A | B) -> A | B |
| 372 | if (match(Op1, m_Or(m_Value(A), m_Value(B))) && |
| 373 | (A == Op0 || B == Op0)) |
| 374 | return Op1; |
| 375 | |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 376 | // If the operation is with the result of a select instruction, check whether |
| 377 | // operating on either branch of the select always yields the same value. |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 378 | if (MaxRecurse && (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))) |
| 379 | if (Value *V = ThreadBinOpOverSelect(Instruction::Or, Op0, Op1, TD, |
| 380 | MaxRecurse-1)) |
| 381 | return V; |
| 382 | |
| 383 | // If the operation is with the result of a phi instruction, check whether |
| 384 | // operating on all incoming values of the phi always yields the same value. |
| 385 | if (MaxRecurse && (isa<PHINode>(Op0) || isa<PHINode>(Op1))) |
| 386 | if (Value *V = ThreadBinOpOverPHI(Instruction::Or, Op0, Op1, TD, |
| 387 | MaxRecurse-1)) |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 388 | return V; |
| 389 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 390 | return 0; |
| 391 | } |
| 392 | |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 393 | Value *llvm::SimplifyOrInst(Value *Op0, Value *Op1, const TargetData *TD) { |
| 394 | return ::SimplifyOrInst(Op0, Op1, TD, MaxRecursionDepth); |
| 395 | } |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 396 | |
Chris Lattner | 210c5d4 | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 397 | static const Type *GetCompareTy(Value *Op) { |
| 398 | return CmpInst::makeCmpResultType(Op->getType()); |
| 399 | } |
| 400 | |
Chris Lattner | 9dbb429 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 401 | /// SimplifyICmpInst - Given operands for an ICmpInst, see if we can |
| 402 | /// fold the result. If not, this returns null. |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 403 | static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
| 404 | const TargetData *TD, unsigned MaxRecurse) { |
Chris Lattner | 9f3c25a | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 405 | CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate; |
Chris Lattner | 9dbb429 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 406 | assert(CmpInst::isIntPredicate(Pred) && "Not an integer compare!"); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 407 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 408 | if (Constant *CLHS = dyn_cast<Constant>(LHS)) { |
Chris Lattner | 8f73dea | 2009-11-09 23:06:58 +0000 | [diff] [blame] | 409 | if (Constant *CRHS = dyn_cast<Constant>(RHS)) |
| 410 | return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, TD); |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 411 | |
| 412 | // If we have a constant, make sure it is on the RHS. |
| 413 | std::swap(LHS, RHS); |
| 414 | Pred = CmpInst::getSwappedPredicate(Pred); |
| 415 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 416 | |
Chris Lattner | 210c5d4 | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 417 | // ITy - This is the return type of the compare we're considering. |
| 418 | const Type *ITy = GetCompareTy(LHS); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 419 | |
Chris Lattner | 210c5d4 | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 420 | // icmp X, X -> true/false |
Chris Lattner | c8e14b3 | 2010-03-03 19:46:03 +0000 | [diff] [blame] | 421 | // X icmp undef -> true/false. For example, icmp ugt %X, undef -> false |
| 422 | // because X could be 0. |
| 423 | if (LHS == RHS || isa<UndefValue>(RHS)) |
Chris Lattner | 210c5d4 | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 424 | return ConstantInt::get(ITy, CmpInst::isTrueWhenEqual(Pred)); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 425 | |
Chris Lattner | 210c5d4 | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 426 | // icmp <global/alloca*/null>, <global/alloca*/null> - Global/Stack value |
| 427 | // addresses never equal each other! We already know that Op0 != Op1. |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 428 | if ((isa<GlobalValue>(LHS) || isa<AllocaInst>(LHS) || |
Chris Lattner | 210c5d4 | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 429 | isa<ConstantPointerNull>(LHS)) && |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 430 | (isa<GlobalValue>(RHS) || isa<AllocaInst>(RHS) || |
Chris Lattner | 210c5d4 | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 431 | isa<ConstantPointerNull>(RHS))) |
| 432 | return ConstantInt::get(ITy, CmpInst::isFalseWhenEqual(Pred)); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 433 | |
Chris Lattner | 210c5d4 | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 434 | // See if we are doing a comparison with a constant. |
| 435 | if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) { |
| 436 | // If we have an icmp le or icmp ge instruction, turn it into the |
| 437 | // appropriate icmp lt or icmp gt instruction. This allows us to rely on |
| 438 | // them being folded in the code below. |
| 439 | switch (Pred) { |
| 440 | default: break; |
| 441 | case ICmpInst::ICMP_ULE: |
| 442 | if (CI->isMaxValue(false)) // A <=u MAX -> TRUE |
| 443 | return ConstantInt::getTrue(CI->getContext()); |
| 444 | break; |
| 445 | case ICmpInst::ICMP_SLE: |
| 446 | if (CI->isMaxValue(true)) // A <=s MAX -> TRUE |
| 447 | return ConstantInt::getTrue(CI->getContext()); |
| 448 | break; |
| 449 | case ICmpInst::ICMP_UGE: |
| 450 | if (CI->isMinValue(false)) // A >=u MIN -> TRUE |
| 451 | return ConstantInt::getTrue(CI->getContext()); |
| 452 | break; |
| 453 | case ICmpInst::ICMP_SGE: |
| 454 | if (CI->isMinValue(true)) // A >=s MIN -> TRUE |
| 455 | return ConstantInt::getTrue(CI->getContext()); |
| 456 | break; |
| 457 | } |
Chris Lattner | 210c5d4 | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 458 | } |
Duncan Sands | 1ac7c99 | 2010-11-07 16:12:23 +0000 | [diff] [blame] | 459 | |
| 460 | // If the comparison is with the result of a select instruction, check whether |
| 461 | // comparing with either branch of the select always yields the same value. |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 462 | if (MaxRecurse && (isa<SelectInst>(LHS) || isa<SelectInst>(RHS))) |
| 463 | if (Value *V = ThreadCmpOverSelect(Pred, LHS, RHS, TD, MaxRecurse-1)) |
| 464 | return V; |
| 465 | |
| 466 | // If the comparison is with the result of a phi instruction, check whether |
| 467 | // doing the compare with each incoming phi value yields a common result. |
| 468 | if (MaxRecurse && (isa<PHINode>(LHS) || isa<PHINode>(RHS))) |
| 469 | if (Value *V = ThreadCmpOverPHI(Pred, LHS, RHS, TD, MaxRecurse-1)) |
Duncan Sands | 3bbb0cc | 2010-11-09 17:25:51 +0000 | [diff] [blame] | 470 | return V; |
Duncan Sands | 1ac7c99 | 2010-11-07 16:12:23 +0000 | [diff] [blame] | 471 | |
Chris Lattner | 9f3c25a | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 472 | return 0; |
| 473 | } |
| 474 | |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 475 | Value *llvm::SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
| 476 | const TargetData *TD) { |
| 477 | return ::SimplifyICmpInst(Predicate, LHS, RHS, TD, MaxRecursionDepth); |
| 478 | } |
| 479 | |
Chris Lattner | 9dbb429 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 480 | /// SimplifyFCmpInst - Given operands for an FCmpInst, see if we can |
| 481 | /// fold the result. If not, this returns null. |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 482 | static Value *SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
| 483 | const TargetData *TD, unsigned MaxRecurse) { |
Chris Lattner | 9dbb429 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 484 | CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate; |
| 485 | assert(CmpInst::isFPPredicate(Pred) && "Not an FP compare!"); |
| 486 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 487 | if (Constant *CLHS = dyn_cast<Constant>(LHS)) { |
Chris Lattner | 9dbb429 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 488 | if (Constant *CRHS = dyn_cast<Constant>(RHS)) |
| 489 | return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, TD); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 490 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 491 | // If we have a constant, make sure it is on the RHS. |
| 492 | std::swap(LHS, RHS); |
| 493 | Pred = CmpInst::getSwappedPredicate(Pred); |
| 494 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 495 | |
Chris Lattner | 210c5d4 | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 496 | // Fold trivial predicates. |
| 497 | if (Pred == FCmpInst::FCMP_FALSE) |
| 498 | return ConstantInt::get(GetCompareTy(LHS), 0); |
| 499 | if (Pred == FCmpInst::FCMP_TRUE) |
| 500 | return ConstantInt::get(GetCompareTy(LHS), 1); |
| 501 | |
Chris Lattner | 210c5d4 | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 502 | if (isa<UndefValue>(RHS)) // fcmp pred X, undef -> undef |
| 503 | return UndefValue::get(GetCompareTy(LHS)); |
| 504 | |
| 505 | // fcmp x,x -> true/false. Not all compares are foldable. |
| 506 | if (LHS == RHS) { |
| 507 | if (CmpInst::isTrueWhenEqual(Pred)) |
| 508 | return ConstantInt::get(GetCompareTy(LHS), 1); |
| 509 | if (CmpInst::isFalseWhenEqual(Pred)) |
| 510 | return ConstantInt::get(GetCompareTy(LHS), 0); |
| 511 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 512 | |
Chris Lattner | 210c5d4 | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 513 | // Handle fcmp with constant RHS |
| 514 | if (Constant *RHSC = dyn_cast<Constant>(RHS)) { |
| 515 | // If the constant is a nan, see if we can fold the comparison based on it. |
| 516 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) { |
| 517 | if (CFP->getValueAPF().isNaN()) { |
| 518 | if (FCmpInst::isOrdered(Pred)) // True "if ordered and foo" |
| 519 | return ConstantInt::getFalse(CFP->getContext()); |
| 520 | assert(FCmpInst::isUnordered(Pred) && |
| 521 | "Comparison must be either ordered or unordered!"); |
| 522 | // True if unordered. |
| 523 | return ConstantInt::getTrue(CFP->getContext()); |
| 524 | } |
Dan Gohman | 6b617a7 | 2010-02-22 04:06:03 +0000 | [diff] [blame] | 525 | // Check whether the constant is an infinity. |
| 526 | if (CFP->getValueAPF().isInfinity()) { |
| 527 | if (CFP->getValueAPF().isNegative()) { |
| 528 | switch (Pred) { |
| 529 | case FCmpInst::FCMP_OLT: |
| 530 | // No value is ordered and less than negative infinity. |
| 531 | return ConstantInt::getFalse(CFP->getContext()); |
| 532 | case FCmpInst::FCMP_UGE: |
| 533 | // All values are unordered with or at least negative infinity. |
| 534 | return ConstantInt::getTrue(CFP->getContext()); |
| 535 | default: |
| 536 | break; |
| 537 | } |
| 538 | } else { |
| 539 | switch (Pred) { |
| 540 | case FCmpInst::FCMP_OGT: |
| 541 | // No value is ordered and greater than infinity. |
| 542 | return ConstantInt::getFalse(CFP->getContext()); |
| 543 | case FCmpInst::FCMP_ULE: |
| 544 | // All values are unordered with and at most infinity. |
| 545 | return ConstantInt::getTrue(CFP->getContext()); |
| 546 | default: |
| 547 | break; |
| 548 | } |
| 549 | } |
| 550 | } |
Chris Lattner | 210c5d4 | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 551 | } |
| 552 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 553 | |
Duncan Sands | 92826de | 2010-11-07 16:46:25 +0000 | [diff] [blame] | 554 | // If the comparison is with the result of a select instruction, check whether |
| 555 | // comparing with either branch of the select always yields the same value. |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 556 | if (MaxRecurse && (isa<SelectInst>(LHS) || isa<SelectInst>(RHS))) |
| 557 | if (Value *V = ThreadCmpOverSelect(Pred, LHS, RHS, TD, MaxRecurse-1)) |
| 558 | return V; |
| 559 | |
| 560 | // If the comparison is with the result of a phi instruction, check whether |
| 561 | // doing the compare with each incoming phi value yields a common result. |
| 562 | if (MaxRecurse && (isa<PHINode>(LHS) || isa<PHINode>(RHS))) |
| 563 | if (Value *V = ThreadCmpOverPHI(Pred, LHS, RHS, TD, MaxRecurse-1)) |
Duncan Sands | 3bbb0cc | 2010-11-09 17:25:51 +0000 | [diff] [blame] | 564 | return V; |
Duncan Sands | 92826de | 2010-11-07 16:46:25 +0000 | [diff] [blame] | 565 | |
Chris Lattner | 9dbb429 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 566 | return 0; |
| 567 | } |
| 568 | |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 569 | Value *llvm::SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
| 570 | const TargetData *TD) { |
| 571 | return ::SimplifyFCmpInst(Predicate, LHS, RHS, TD, MaxRecursionDepth); |
| 572 | } |
| 573 | |
Chris Lattner | 0475426 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 574 | /// SimplifySelectInst - Given operands for a SelectInst, see if we can fold |
| 575 | /// the result. If not, this returns null. |
| 576 | Value *llvm::SimplifySelectInst(Value *CondVal, Value *TrueVal, Value *FalseVal, |
| 577 | const TargetData *TD) { |
| 578 | // select true, X, Y -> X |
| 579 | // select false, X, Y -> Y |
| 580 | if (ConstantInt *CB = dyn_cast<ConstantInt>(CondVal)) |
| 581 | return CB->getZExtValue() ? TrueVal : FalseVal; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 582 | |
Chris Lattner | 0475426 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 583 | // select C, X, X -> X |
| 584 | if (TrueVal == FalseVal) |
| 585 | return TrueVal; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 586 | |
Chris Lattner | 0475426 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 587 | if (isa<UndefValue>(TrueVal)) // select C, undef, X -> X |
| 588 | return FalseVal; |
| 589 | if (isa<UndefValue>(FalseVal)) // select C, X, undef -> X |
| 590 | return TrueVal; |
| 591 | if (isa<UndefValue>(CondVal)) { // select undef, X, Y -> X or Y |
| 592 | if (isa<Constant>(TrueVal)) |
| 593 | return TrueVal; |
| 594 | return FalseVal; |
| 595 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 596 | |
Chris Lattner | 0475426 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 597 | return 0; |
| 598 | } |
| 599 | |
| 600 | |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 601 | /// SimplifyGEPInst - Given operands for an GetElementPtrInst, see if we can |
| 602 | /// fold the result. If not, this returns null. |
| 603 | Value *llvm::SimplifyGEPInst(Value *const *Ops, unsigned NumOps, |
| 604 | const TargetData *TD) { |
| 605 | // getelementptr P -> P. |
| 606 | if (NumOps == 1) |
| 607 | return Ops[0]; |
| 608 | |
| 609 | // TODO. |
| 610 | //if (isa<UndefValue>(Ops[0])) |
| 611 | // return UndefValue::get(GEP.getType()); |
| 612 | |
| 613 | // getelementptr P, 0 -> P. |
| 614 | if (NumOps == 2) |
| 615 | if (ConstantInt *C = dyn_cast<ConstantInt>(Ops[1])) |
| 616 | if (C->isZero()) |
| 617 | return Ops[0]; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 618 | |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 619 | // Check to see if this is constant foldable. |
| 620 | for (unsigned i = 0; i != NumOps; ++i) |
| 621 | if (!isa<Constant>(Ops[i])) |
| 622 | return 0; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 623 | |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 624 | return ConstantExpr::getGetElementPtr(cast<Constant>(Ops[0]), |
| 625 | (Constant *const*)Ops+1, NumOps-1); |
| 626 | } |
| 627 | |
| 628 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 629 | //=== Helper functions for higher up the class hierarchy. |
Chris Lattner | 9dbb429 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 630 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 631 | /// SimplifyBinOp - Given operands for a BinaryOperator, see if we can |
| 632 | /// fold the result. If not, this returns null. |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 633 | static Value *SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, |
| 634 | const TargetData *TD, unsigned MaxRecurse) { |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 635 | switch (Opcode) { |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 636 | case Instruction::And: return SimplifyAndInst(LHS, RHS, TD, MaxRecurse); |
| 637 | case Instruction::Or: return SimplifyOrInst(LHS, RHS, TD, MaxRecurse); |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 638 | default: |
| 639 | if (Constant *CLHS = dyn_cast<Constant>(LHS)) |
| 640 | if (Constant *CRHS = dyn_cast<Constant>(RHS)) { |
| 641 | Constant *COps[] = {CLHS, CRHS}; |
| 642 | return ConstantFoldInstOperands(Opcode, LHS->getType(), COps, 2, TD); |
| 643 | } |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 644 | |
| 645 | // If the operation is with the result of a select instruction, check whether |
| 646 | // operating on either branch of the select always yields the same value. |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 647 | if (MaxRecurse && (isa<SelectInst>(LHS) || isa<SelectInst>(RHS))) |
| 648 | if (Value *V = ThreadBinOpOverSelect(Opcode, LHS, RHS, TD, MaxRecurse-1)) |
| 649 | return V; |
| 650 | |
| 651 | // If the operation is with the result of a phi instruction, check whether |
| 652 | // operating on all incoming values of the phi always yields the same value. |
| 653 | if (MaxRecurse && (isa<PHINode>(LHS) || isa<PHINode>(RHS))) |
| 654 | if (Value *V = ThreadBinOpOverPHI(Opcode, LHS, RHS, TD, MaxRecurse-1)) |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 655 | return V; |
| 656 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 657 | return 0; |
| 658 | } |
| 659 | } |
Chris Lattner | 9dbb429 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 660 | |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 661 | Value *llvm::SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 662 | const TargetData *TD) { |
| 663 | return ::SimplifyBinOp(Opcode, LHS, RHS, TD, MaxRecursionDepth); |
Chris Lattner | 9dbb429 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 664 | } |
| 665 | |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 666 | /// SimplifyCmpInst - Given operands for a CmpInst, see if we can |
| 667 | /// fold the result. |
| 668 | static Value *SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
| 669 | const TargetData *TD, unsigned MaxRecurse) { |
| 670 | if (CmpInst::isIntPredicate((CmpInst::Predicate)Predicate)) |
| 671 | return SimplifyICmpInst(Predicate, LHS, RHS, TD, MaxRecurse); |
| 672 | return SimplifyFCmpInst(Predicate, LHS, RHS, TD, MaxRecurse); |
| 673 | } |
| 674 | |
| 675 | Value *llvm::SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
| 676 | const TargetData *TD) { |
| 677 | return ::SimplifyCmpInst(Predicate, LHS, RHS, TD, MaxRecursionDepth); |
| 678 | } |
Chris Lattner | e345378 | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 679 | |
| 680 | /// SimplifyInstruction - See if we can compute a simplified version of this |
| 681 | /// instruction. If not, this returns null. |
Duncan Sands | eff0581 | 2010-11-14 18:36:10 +0000 | [diff] [blame] | 682 | Value *llvm::SimplifyInstruction(Instruction *I, const TargetData *TD, |
| 683 | const DominatorTree *DT) { |
Chris Lattner | e345378 | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 684 | switch (I->getOpcode()) { |
| 685 | default: |
| 686 | return ConstantFoldInstruction(I, TD); |
Chris Lattner | 8aee8ef | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 687 | case Instruction::Add: |
Owen Anderson | 4e282de | 2010-09-16 20:51:41 +0000 | [diff] [blame] | 688 | return SimplifyAddInst(I->getOperand(0), I->getOperand(1), |
| 689 | cast<BinaryOperator>(I)->hasNoSignedWrap(), |
| 690 | cast<BinaryOperator>(I)->hasNoUnsignedWrap(), TD); |
Chris Lattner | e345378 | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 691 | case Instruction::And: |
Owen Anderson | 4e282de | 2010-09-16 20:51:41 +0000 | [diff] [blame] | 692 | return SimplifyAndInst(I->getOperand(0), I->getOperand(1), TD); |
Chris Lattner | e345378 | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 693 | case Instruction::Or: |
Owen Anderson | 4e282de | 2010-09-16 20:51:41 +0000 | [diff] [blame] | 694 | return SimplifyOrInst(I->getOperand(0), I->getOperand(1), TD); |
Chris Lattner | e345378 | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 695 | case Instruction::ICmp: |
Owen Anderson | 4e282de | 2010-09-16 20:51:41 +0000 | [diff] [blame] | 696 | return SimplifyICmpInst(cast<ICmpInst>(I)->getPredicate(), |
| 697 | I->getOperand(0), I->getOperand(1), TD); |
Chris Lattner | e345378 | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 698 | case Instruction::FCmp: |
Owen Anderson | 4e282de | 2010-09-16 20:51:41 +0000 | [diff] [blame] | 699 | return SimplifyFCmpInst(cast<FCmpInst>(I)->getPredicate(), |
| 700 | I->getOperand(0), I->getOperand(1), TD); |
Chris Lattner | 0475426 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 701 | case Instruction::Select: |
Owen Anderson | 4e282de | 2010-09-16 20:51:41 +0000 | [diff] [blame] | 702 | return SimplifySelectInst(I->getOperand(0), I->getOperand(1), |
Chris Lattner | 0475426 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 703 | I->getOperand(2), TD); |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 704 | case Instruction::GetElementPtr: { |
| 705 | SmallVector<Value*, 8> Ops(I->op_begin(), I->op_end()); |
Owen Anderson | 4e282de | 2010-09-16 20:51:41 +0000 | [diff] [blame] | 706 | return SimplifyGEPInst(&Ops[0], Ops.size(), TD); |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 707 | } |
Duncan Sands | cd6636c | 2010-11-14 13:30:18 +0000 | [diff] [blame] | 708 | case Instruction::PHI: |
Duncan Sands | eff0581 | 2010-11-14 18:36:10 +0000 | [diff] [blame] | 709 | return cast<PHINode>(I)->hasConstantValue(DT); |
Chris Lattner | e345378 | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 710 | } |
| 711 | } |
| 712 | |
Chris Lattner | 40d8c28 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 713 | /// ReplaceAndSimplifyAllUses - Perform From->replaceAllUsesWith(To) and then |
| 714 | /// delete the From instruction. In addition to a basic RAUW, this does a |
| 715 | /// recursive simplification of the newly formed instructions. This catches |
| 716 | /// things where one simplification exposes other opportunities. This only |
| 717 | /// simplifies and deletes scalar operations, it does not change the CFG. |
| 718 | /// |
| 719 | void llvm::ReplaceAndSimplifyAllUses(Instruction *From, Value *To, |
Duncan Sands | eff0581 | 2010-11-14 18:36:10 +0000 | [diff] [blame] | 720 | const TargetData *TD, |
| 721 | const DominatorTree *DT) { |
Chris Lattner | 40d8c28 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 722 | assert(From != To && "ReplaceAndSimplifyAllUses(X,X) is not valid!"); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 723 | |
Chris Lattner | d2bfe54 | 2010-07-15 06:36:08 +0000 | [diff] [blame] | 724 | // FromHandle/ToHandle - This keeps a WeakVH on the from/to values so that |
| 725 | // we can know if it gets deleted out from under us or replaced in a |
| 726 | // recursive simplification. |
Chris Lattner | 40d8c28 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 727 | WeakVH FromHandle(From); |
Chris Lattner | d2bfe54 | 2010-07-15 06:36:08 +0000 | [diff] [blame] | 728 | WeakVH ToHandle(To); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 729 | |
Chris Lattner | 40d8c28 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 730 | while (!From->use_empty()) { |
| 731 | // Update the instruction to use the new value. |
Chris Lattner | d2bfe54 | 2010-07-15 06:36:08 +0000 | [diff] [blame] | 732 | Use &TheUse = From->use_begin().getUse(); |
| 733 | Instruction *User = cast<Instruction>(TheUse.getUser()); |
| 734 | TheUse = To; |
| 735 | |
| 736 | // Check to see if the instruction can be folded due to the operand |
| 737 | // replacement. For example changing (or X, Y) into (or X, -1) can replace |
| 738 | // the 'or' with -1. |
| 739 | Value *SimplifiedVal; |
| 740 | { |
| 741 | // Sanity check to make sure 'User' doesn't dangle across |
| 742 | // SimplifyInstruction. |
| 743 | AssertingVH<> UserHandle(User); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 744 | |
Duncan Sands | eff0581 | 2010-11-14 18:36:10 +0000 | [diff] [blame] | 745 | SimplifiedVal = SimplifyInstruction(User, TD, DT); |
Chris Lattner | d2bfe54 | 2010-07-15 06:36:08 +0000 | [diff] [blame] | 746 | if (SimplifiedVal == 0) continue; |
Chris Lattner | 40d8c28 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 747 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 748 | |
Chris Lattner | d2bfe54 | 2010-07-15 06:36:08 +0000 | [diff] [blame] | 749 | // Recursively simplify this user to the new value. |
Duncan Sands | eff0581 | 2010-11-14 18:36:10 +0000 | [diff] [blame] | 750 | ReplaceAndSimplifyAllUses(User, SimplifiedVal, TD, DT); |
Chris Lattner | d2bfe54 | 2010-07-15 06:36:08 +0000 | [diff] [blame] | 751 | From = dyn_cast_or_null<Instruction>((Value*)FromHandle); |
| 752 | To = ToHandle; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 753 | |
Chris Lattner | d2bfe54 | 2010-07-15 06:36:08 +0000 | [diff] [blame] | 754 | assert(ToHandle && "To value deleted by recursive simplification?"); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 755 | |
Chris Lattner | d2bfe54 | 2010-07-15 06:36:08 +0000 | [diff] [blame] | 756 | // If the recursive simplification ended up revisiting and deleting |
| 757 | // 'From' then we're done. |
| 758 | if (From == 0) |
| 759 | return; |
Chris Lattner | 40d8c28 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 760 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 761 | |
Chris Lattner | d2bfe54 | 2010-07-15 06:36:08 +0000 | [diff] [blame] | 762 | // If 'From' has value handles referring to it, do a real RAUW to update them. |
| 763 | From->replaceAllUsesWith(To); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 764 | |
Chris Lattner | 40d8c28 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 765 | From->eraseFromParent(); |
| 766 | } |