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