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 |
Duncan Sands | 4cd2ad1 | 2010-11-23 10:50:08 +0000 | [diff] [blame] | 11 | // that do not require creating new instructions. This does constant folding |
| 12 | // ("add i32 1, 1" -> "2") but can also handle non-constant operands, either |
| 13 | // returning a constant ("and i32 %x, 0" -> "0") or an already existing value |
Duncan Sands | ee9a2e3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 14 | // ("and i32 %x, %x" -> "%x"). All operands are assumed to have already been |
| 15 | // simplified: This is usually true and assuming it simplifies the logic (if |
| 16 | // they have not been simplified then results are correct but maybe suboptimal). |
Chris Lattner | 9f3c25a | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 17 | // |
| 18 | //===----------------------------------------------------------------------===// |
| 19 | |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 20 | #define DEBUG_TYPE "instsimplify" |
| 21 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | 9f3c25a | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/InstructionSimplify.h" |
| 23 | #include "llvm/Analysis/ConstantFolding.h" |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/Dominators.h" |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/ValueTracking.h" |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 26 | #include "llvm/Support/PatternMatch.h" |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 27 | #include "llvm/Support/ValueHandle.h" |
Duncan Sands | e60d79f | 2010-11-21 13:53:09 +0000 | [diff] [blame] | 28 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 9f3c25a | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 29 | using namespace llvm; |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 30 | using namespace llvm::PatternMatch; |
Chris Lattner | 9f3c25a | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 31 | |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 32 | #define RecursionLimit 3 |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 33 | |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 34 | STATISTIC(NumExpand, "Number of expansions"); |
| 35 | STATISTIC(NumFactor , "Number of factorizations"); |
| 36 | STATISTIC(NumReassoc, "Number of reassociations"); |
| 37 | |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 38 | static Value *SimplifyAndInst(Value *, Value *, const TargetData *, |
| 39 | const DominatorTree *, unsigned); |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 40 | static Value *SimplifyBinOp(unsigned, Value *, Value *, const TargetData *, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 41 | const DominatorTree *, unsigned); |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 42 | static Value *SimplifyCmpInst(unsigned, Value *, Value *, const TargetData *, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 43 | const DominatorTree *, unsigned); |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 44 | static Value *SimplifyOrInst(Value *, Value *, const TargetData *, |
| 45 | const DominatorTree *, unsigned); |
| 46 | static Value *SimplifyXorInst(Value *, Value *, const TargetData *, |
| 47 | const DominatorTree *, unsigned); |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 48 | |
| 49 | /// ValueDominatesPHI - Does the given value dominate the specified phi node? |
| 50 | static bool ValueDominatesPHI(Value *V, PHINode *P, const DominatorTree *DT) { |
| 51 | Instruction *I = dyn_cast<Instruction>(V); |
| 52 | if (!I) |
| 53 | // Arguments and constants dominate all instructions. |
| 54 | return true; |
| 55 | |
| 56 | // If we have a DominatorTree then do a precise test. |
| 57 | if (DT) |
| 58 | return DT->dominates(I, P); |
| 59 | |
| 60 | // Otherwise, if the instruction is in the entry block, and is not an invoke, |
| 61 | // then it obviously dominates all phi nodes. |
| 62 | if (I->getParent() == &I->getParent()->getParent()->getEntryBlock() && |
| 63 | !isa<InvokeInst>(I)) |
| 64 | return true; |
| 65 | |
| 66 | return false; |
| 67 | } |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 68 | |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 69 | /// ExpandBinOp - Simplify "A op (B op' C)" by distributing op over op', turning |
| 70 | /// it into "(A op B) op' (A op C)". Here "op" is given by Opcode and "op'" is |
| 71 | /// given by OpcodeToExpand, while "A" corresponds to LHS and "B op' C" to RHS. |
| 72 | /// Also performs the transform "(A op' B) op C" -> "(A op C) op' (B op C)". |
| 73 | /// Returns the simplified value, or null if no simplification was performed. |
| 74 | static Value *ExpandBinOp(unsigned Opcode, Value *LHS, Value *RHS, |
Benjamin Kramer | e21083a | 2010-12-28 13:52:52 +0000 | [diff] [blame] | 75 | unsigned OpcToExpand, const TargetData *TD, |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 76 | const DominatorTree *DT, unsigned MaxRecurse) { |
Benjamin Kramer | e21083a | 2010-12-28 13:52:52 +0000 | [diff] [blame] | 77 | Instruction::BinaryOps OpcodeToExpand = (Instruction::BinaryOps)OpcToExpand; |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 78 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 79 | if (!MaxRecurse--) |
| 80 | return 0; |
| 81 | |
| 82 | // Check whether the expression has the form "(A op' B) op C". |
| 83 | if (BinaryOperator *Op0 = dyn_cast<BinaryOperator>(LHS)) |
| 84 | if (Op0->getOpcode() == OpcodeToExpand) { |
| 85 | // It does! Try turning it into "(A op C) op' (B op C)". |
| 86 | Value *A = Op0->getOperand(0), *B = Op0->getOperand(1), *C = RHS; |
| 87 | // Do "A op C" and "B op C" both simplify? |
| 88 | if (Value *L = SimplifyBinOp(Opcode, A, C, TD, DT, MaxRecurse)) |
| 89 | if (Value *R = SimplifyBinOp(Opcode, B, C, TD, DT, MaxRecurse)) { |
| 90 | // They do! Return "L op' R" if it simplifies or is already available. |
| 91 | // If "L op' R" equals "A op' B" then "L op' R" is just the LHS. |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 92 | if ((L == A && R == B) || (Instruction::isCommutative(OpcodeToExpand) |
| 93 | && L == B && R == A)) { |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 94 | ++NumExpand; |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 95 | return LHS; |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 96 | } |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 97 | // Otherwise return "L op' R" if it simplifies. |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 98 | if (Value *V = SimplifyBinOp(OpcodeToExpand, L, R, TD, DT, |
| 99 | MaxRecurse)) { |
| 100 | ++NumExpand; |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 101 | return V; |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 102 | } |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
| 106 | // Check whether the expression has the form "A op (B op' C)". |
| 107 | if (BinaryOperator *Op1 = dyn_cast<BinaryOperator>(RHS)) |
| 108 | if (Op1->getOpcode() == OpcodeToExpand) { |
| 109 | // It does! Try turning it into "(A op B) op' (A op C)". |
| 110 | Value *A = LHS, *B = Op1->getOperand(0), *C = Op1->getOperand(1); |
| 111 | // Do "A op B" and "A op C" both simplify? |
| 112 | if (Value *L = SimplifyBinOp(Opcode, A, B, TD, DT, MaxRecurse)) |
| 113 | if (Value *R = SimplifyBinOp(Opcode, A, C, TD, DT, MaxRecurse)) { |
| 114 | // They do! Return "L op' R" if it simplifies or is already available. |
| 115 | // If "L op' R" equals "B op' C" then "L op' R" is just the RHS. |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 116 | if ((L == B && R == C) || (Instruction::isCommutative(OpcodeToExpand) |
| 117 | && L == C && R == B)) { |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 118 | ++NumExpand; |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 119 | return RHS; |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 120 | } |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 121 | // Otherwise return "L op' R" if it simplifies. |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 122 | if (Value *V = SimplifyBinOp(OpcodeToExpand, L, R, TD, DT, |
| 123 | MaxRecurse)) { |
| 124 | ++NumExpand; |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 125 | return V; |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 126 | } |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | /// FactorizeBinOp - Simplify "LHS Opcode RHS" by factorizing out a common term |
| 134 | /// using the operation OpCodeToExtract. For example, when Opcode is Add and |
| 135 | /// OpCodeToExtract is Mul then this tries to turn "(A*B)+(A*C)" into "A*(B+C)". |
| 136 | /// Returns the simplified value, or null if no simplification was performed. |
| 137 | static Value *FactorizeBinOp(unsigned Opcode, Value *LHS, Value *RHS, |
Benjamin Kramer | e21083a | 2010-12-28 13:52:52 +0000 | [diff] [blame] | 138 | unsigned OpcToExtract, const TargetData *TD, |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 139 | const DominatorTree *DT, unsigned MaxRecurse) { |
Benjamin Kramer | e21083a | 2010-12-28 13:52:52 +0000 | [diff] [blame] | 140 | Instruction::BinaryOps OpcodeToExtract = (Instruction::BinaryOps)OpcToExtract; |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 141 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 142 | if (!MaxRecurse--) |
| 143 | return 0; |
| 144 | |
| 145 | BinaryOperator *Op0 = dyn_cast<BinaryOperator>(LHS); |
| 146 | BinaryOperator *Op1 = dyn_cast<BinaryOperator>(RHS); |
| 147 | |
| 148 | if (!Op0 || Op0->getOpcode() != OpcodeToExtract || |
| 149 | !Op1 || Op1->getOpcode() != OpcodeToExtract) |
| 150 | return 0; |
| 151 | |
| 152 | // The expression has the form "(A op' B) op (C op' D)". |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 153 | Value *A = Op0->getOperand(0), *B = Op0->getOperand(1); |
| 154 | Value *C = Op1->getOperand(0), *D = Op1->getOperand(1); |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 155 | |
| 156 | // Use left distributivity, i.e. "X op' (Y op Z) = (X op' Y) op (X op' Z)". |
| 157 | // Does the instruction have the form "(A op' B) op (A op' D)" or, in the |
| 158 | // commutative case, "(A op' B) op (C op' A)"? |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 159 | if (A == C || (Instruction::isCommutative(OpcodeToExtract) && A == D)) { |
| 160 | Value *DD = A == C ? D : C; |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 161 | // Form "A op' (B op DD)" if it simplifies completely. |
| 162 | // Does "B op DD" simplify? |
| 163 | if (Value *V = SimplifyBinOp(Opcode, B, DD, TD, DT, MaxRecurse)) { |
| 164 | // It does! Return "A op' V" if it simplifies or is already available. |
Duncan Sands | 1cd05bb | 2010-12-22 17:15:25 +0000 | [diff] [blame] | 165 | // If V equals B then "A op' V" is just the LHS. If V equals DD then |
| 166 | // "A op' V" is just the RHS. |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 167 | if (V == B || V == DD) { |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 168 | ++NumFactor; |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 169 | return V == B ? LHS : RHS; |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 170 | } |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 171 | // Otherwise return "A op' V" if it simplifies. |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 172 | if (Value *W = SimplifyBinOp(OpcodeToExtract, A, V, TD, DT, MaxRecurse)) { |
| 173 | ++NumFactor; |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 174 | return W; |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 175 | } |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
| 179 | // Use right distributivity, i.e. "(X op Y) op' Z = (X op' Z) op (Y op' Z)". |
| 180 | // Does the instruction have the form "(A op' B) op (C op' B)" or, in the |
| 181 | // commutative case, "(A op' B) op (B op' D)"? |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 182 | if (B == D || (Instruction::isCommutative(OpcodeToExtract) && B == C)) { |
| 183 | Value *CC = B == D ? C : D; |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 184 | // Form "(A op CC) op' B" if it simplifies completely.. |
| 185 | // Does "A op CC" simplify? |
| 186 | if (Value *V = SimplifyBinOp(Opcode, A, CC, TD, DT, MaxRecurse)) { |
| 187 | // It does! Return "V op' B" if it simplifies or is already available. |
Duncan Sands | 1cd05bb | 2010-12-22 17:15:25 +0000 | [diff] [blame] | 188 | // If V equals A then "V op' B" is just the LHS. If V equals CC then |
| 189 | // "V op' B" is just the RHS. |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 190 | if (V == A || V == CC) { |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 191 | ++NumFactor; |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 192 | return V == A ? LHS : RHS; |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 193 | } |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 194 | // Otherwise return "V op' B" if it simplifies. |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 195 | if (Value *W = SimplifyBinOp(OpcodeToExtract, V, B, TD, DT, MaxRecurse)) { |
| 196 | ++NumFactor; |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 197 | return W; |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 198 | } |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | /// SimplifyAssociativeBinOp - Generic simplifications for associative binary |
| 206 | /// operations. Returns the simpler value, or null if none was found. |
Benjamin Kramer | e21083a | 2010-12-28 13:52:52 +0000 | [diff] [blame] | 207 | static Value *SimplifyAssociativeBinOp(unsigned Opc, Value *LHS, Value *RHS, |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 208 | const TargetData *TD, |
| 209 | const DominatorTree *DT, |
| 210 | unsigned MaxRecurse) { |
Benjamin Kramer | e21083a | 2010-12-28 13:52:52 +0000 | [diff] [blame] | 211 | Instruction::BinaryOps Opcode = (Instruction::BinaryOps)Opc; |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 212 | assert(Instruction::isAssociative(Opcode) && "Not an associative operation!"); |
| 213 | |
| 214 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 215 | if (!MaxRecurse--) |
| 216 | return 0; |
| 217 | |
| 218 | BinaryOperator *Op0 = dyn_cast<BinaryOperator>(LHS); |
| 219 | BinaryOperator *Op1 = dyn_cast<BinaryOperator>(RHS); |
| 220 | |
| 221 | // Transform: "(A op B) op C" ==> "A op (B op C)" if it simplifies completely. |
| 222 | if (Op0 && Op0->getOpcode() == Opcode) { |
| 223 | Value *A = Op0->getOperand(0); |
| 224 | Value *B = Op0->getOperand(1); |
| 225 | Value *C = RHS; |
| 226 | |
| 227 | // Does "B op C" simplify? |
| 228 | if (Value *V = SimplifyBinOp(Opcode, B, C, TD, DT, MaxRecurse)) { |
| 229 | // It does! Return "A op V" if it simplifies or is already available. |
| 230 | // If V equals B then "A op V" is just the LHS. |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 231 | if (V == B) return LHS; |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 232 | // Otherwise return "A op V" if it simplifies. |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 233 | if (Value *W = SimplifyBinOp(Opcode, A, V, TD, DT, MaxRecurse)) { |
| 234 | ++NumReassoc; |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 235 | return W; |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 236 | } |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | |
| 240 | // Transform: "A op (B op C)" ==> "(A op B) op C" if it simplifies completely. |
| 241 | if (Op1 && Op1->getOpcode() == Opcode) { |
| 242 | Value *A = LHS; |
| 243 | Value *B = Op1->getOperand(0); |
| 244 | Value *C = Op1->getOperand(1); |
| 245 | |
| 246 | // Does "A op B" simplify? |
| 247 | if (Value *V = SimplifyBinOp(Opcode, A, B, TD, DT, MaxRecurse)) { |
| 248 | // It does! Return "V op C" if it simplifies or is already available. |
| 249 | // If V equals B then "V op C" is just the RHS. |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 250 | if (V == B) return RHS; |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 251 | // Otherwise return "V op C" if it simplifies. |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 252 | if (Value *W = SimplifyBinOp(Opcode, V, C, TD, DT, MaxRecurse)) { |
| 253 | ++NumReassoc; |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 254 | return W; |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 255 | } |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | |
| 259 | // The remaining transforms require commutativity as well as associativity. |
| 260 | if (!Instruction::isCommutative(Opcode)) |
| 261 | return 0; |
| 262 | |
| 263 | // Transform: "(A op B) op C" ==> "(C op A) op B" if it simplifies completely. |
| 264 | if (Op0 && Op0->getOpcode() == Opcode) { |
| 265 | Value *A = Op0->getOperand(0); |
| 266 | Value *B = Op0->getOperand(1); |
| 267 | Value *C = RHS; |
| 268 | |
| 269 | // Does "C op A" simplify? |
| 270 | if (Value *V = SimplifyBinOp(Opcode, C, A, TD, DT, MaxRecurse)) { |
| 271 | // It does! Return "V op B" if it simplifies or is already available. |
| 272 | // If V equals A then "V op B" is just the LHS. |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 273 | if (V == A) return LHS; |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 274 | // Otherwise return "V op B" if it simplifies. |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 275 | if (Value *W = SimplifyBinOp(Opcode, V, B, TD, DT, MaxRecurse)) { |
| 276 | ++NumReassoc; |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 277 | return W; |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 278 | } |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |
| 282 | // Transform: "A op (B op C)" ==> "B op (C op A)" if it simplifies completely. |
| 283 | if (Op1 && Op1->getOpcode() == Opcode) { |
| 284 | Value *A = LHS; |
| 285 | Value *B = Op1->getOperand(0); |
| 286 | Value *C = Op1->getOperand(1); |
| 287 | |
| 288 | // Does "C op A" simplify? |
| 289 | if (Value *V = SimplifyBinOp(Opcode, C, A, TD, DT, MaxRecurse)) { |
| 290 | // It does! Return "B op V" if it simplifies or is already available. |
| 291 | // If V equals C then "B op V" is just the RHS. |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 292 | if (V == C) return RHS; |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 293 | // Otherwise return "B op V" if it simplifies. |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 294 | if (Value *W = SimplifyBinOp(Opcode, B, V, TD, DT, MaxRecurse)) { |
| 295 | ++NumReassoc; |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 296 | return W; |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 297 | } |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | |
| 301 | return 0; |
| 302 | } |
| 303 | |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 304 | /// ThreadBinOpOverSelect - In the case of a binary operation with a select |
| 305 | /// instruction as an operand, try to simplify the binop by seeing whether |
| 306 | /// evaluating it on both branches of the select results in the same value. |
| 307 | /// Returns the common value if so, otherwise returns null. |
| 308 | static Value *ThreadBinOpOverSelect(unsigned Opcode, Value *LHS, Value *RHS, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 309 | const TargetData *TD, |
| 310 | const DominatorTree *DT, |
| 311 | unsigned MaxRecurse) { |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 312 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 313 | if (!MaxRecurse--) |
| 314 | return 0; |
| 315 | |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 316 | SelectInst *SI; |
| 317 | if (isa<SelectInst>(LHS)) { |
| 318 | SI = cast<SelectInst>(LHS); |
| 319 | } else { |
| 320 | assert(isa<SelectInst>(RHS) && "No select instruction operand!"); |
| 321 | SI = cast<SelectInst>(RHS); |
| 322 | } |
| 323 | |
| 324 | // Evaluate the BinOp on the true and false branches of the select. |
| 325 | Value *TV; |
| 326 | Value *FV; |
| 327 | if (SI == LHS) { |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 328 | TV = SimplifyBinOp(Opcode, SI->getTrueValue(), RHS, TD, DT, MaxRecurse); |
| 329 | FV = SimplifyBinOp(Opcode, SI->getFalseValue(), RHS, TD, DT, MaxRecurse); |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 330 | } else { |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 331 | TV = SimplifyBinOp(Opcode, LHS, SI->getTrueValue(), TD, DT, MaxRecurse); |
| 332 | FV = SimplifyBinOp(Opcode, LHS, SI->getFalseValue(), TD, DT, MaxRecurse); |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Duncan Sands | 7cf85e7 | 2011-01-01 16:12:09 +0000 | [diff] [blame] | 335 | // If they simplified to the same value, then return the common value. |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 336 | // If they both failed to simplify then return null. |
| 337 | if (TV == FV) |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 338 | return TV; |
| 339 | |
| 340 | // If one branch simplified to undef, return the other one. |
| 341 | if (TV && isa<UndefValue>(TV)) |
| 342 | return FV; |
| 343 | if (FV && isa<UndefValue>(FV)) |
| 344 | return TV; |
| 345 | |
| 346 | // If applying the operation did not change the true and false select values, |
| 347 | // then the result of the binop is the select itself. |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 348 | if (TV == SI->getTrueValue() && FV == SI->getFalseValue()) |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 349 | return SI; |
| 350 | |
| 351 | // If one branch simplified and the other did not, and the simplified |
| 352 | // value is equal to the unsimplified one, return the simplified value. |
| 353 | // For example, select (cond, X, X & Z) & Z -> X & Z. |
| 354 | if ((FV && !TV) || (TV && !FV)) { |
| 355 | // Check that the simplified value has the form "X op Y" where "op" is the |
| 356 | // same as the original operation. |
| 357 | Instruction *Simplified = dyn_cast<Instruction>(FV ? FV : TV); |
| 358 | if (Simplified && Simplified->getOpcode() == Opcode) { |
| 359 | // The value that didn't simplify is "UnsimplifiedLHS op UnsimplifiedRHS". |
| 360 | // We already know that "op" is the same as for the simplified value. See |
| 361 | // if the operands match too. If so, return the simplified value. |
| 362 | Value *UnsimplifiedBranch = FV ? SI->getTrueValue() : SI->getFalseValue(); |
| 363 | Value *UnsimplifiedLHS = SI == LHS ? UnsimplifiedBranch : LHS; |
| 364 | Value *UnsimplifiedRHS = SI == LHS ? RHS : UnsimplifiedBranch; |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 365 | if (Simplified->getOperand(0) == UnsimplifiedLHS && |
| 366 | Simplified->getOperand(1) == UnsimplifiedRHS) |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 367 | return Simplified; |
| 368 | if (Simplified->isCommutative() && |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 369 | Simplified->getOperand(1) == UnsimplifiedLHS && |
| 370 | Simplified->getOperand(0) == UnsimplifiedRHS) |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 371 | return Simplified; |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | return 0; |
| 376 | } |
| 377 | |
| 378 | /// ThreadCmpOverSelect - In the case of a comparison with a select instruction, |
| 379 | /// try to simplify the comparison by seeing whether both branches of the select |
| 380 | /// result in the same value. Returns the common value if so, otherwise returns |
| 381 | /// null. |
| 382 | static Value *ThreadCmpOverSelect(CmpInst::Predicate Pred, Value *LHS, |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 383 | Value *RHS, const TargetData *TD, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 384 | const DominatorTree *DT, |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 385 | unsigned MaxRecurse) { |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 386 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 387 | if (!MaxRecurse--) |
| 388 | return 0; |
| 389 | |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 390 | // Make sure the select is on the LHS. |
| 391 | if (!isa<SelectInst>(LHS)) { |
| 392 | std::swap(LHS, RHS); |
| 393 | Pred = CmpInst::getSwappedPredicate(Pred); |
| 394 | } |
| 395 | assert(isa<SelectInst>(LHS) && "Not comparing with a select instruction!"); |
| 396 | SelectInst *SI = cast<SelectInst>(LHS); |
| 397 | |
| 398 | // Now that we have "cmp select(cond, TV, FV), RHS", analyse it. |
| 399 | // Does "cmp TV, RHS" simplify? |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 400 | if (Value *TCmp = SimplifyCmpInst(Pred, SI->getTrueValue(), RHS, TD, DT, |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 401 | MaxRecurse)) |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 402 | // It does! Does "cmp FV, RHS" simplify? |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 403 | if (Value *FCmp = SimplifyCmpInst(Pred, SI->getFalseValue(), RHS, TD, DT, |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 404 | MaxRecurse)) |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 405 | // It does! If they simplified to the same value, then use it as the |
| 406 | // result of the original comparison. |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 407 | if (TCmp == FCmp) |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 408 | return TCmp; |
| 409 | return 0; |
| 410 | } |
| 411 | |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 412 | /// ThreadBinOpOverPHI - In the case of a binary operation with an operand that |
| 413 | /// is a PHI instruction, try to simplify the binop by seeing whether evaluating |
| 414 | /// it on the incoming phi values yields the same result for every value. If so |
| 415 | /// returns the common value, otherwise returns null. |
| 416 | static Value *ThreadBinOpOverPHI(unsigned Opcode, Value *LHS, Value *RHS, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 417 | const TargetData *TD, const DominatorTree *DT, |
| 418 | unsigned MaxRecurse) { |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 419 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 420 | if (!MaxRecurse--) |
| 421 | return 0; |
| 422 | |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 423 | PHINode *PI; |
| 424 | if (isa<PHINode>(LHS)) { |
| 425 | PI = cast<PHINode>(LHS); |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 426 | // Bail out if RHS and the phi may be mutually interdependent due to a loop. |
| 427 | if (!ValueDominatesPHI(RHS, PI, DT)) |
| 428 | return 0; |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 429 | } else { |
| 430 | assert(isa<PHINode>(RHS) && "No PHI instruction operand!"); |
| 431 | PI = cast<PHINode>(RHS); |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 432 | // Bail out if LHS and the phi may be mutually interdependent due to a loop. |
| 433 | if (!ValueDominatesPHI(LHS, PI, DT)) |
| 434 | return 0; |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | // Evaluate the BinOp on the incoming phi values. |
| 438 | Value *CommonValue = 0; |
| 439 | for (unsigned i = 0, e = PI->getNumIncomingValues(); i != e; ++i) { |
Duncan Sands | 5520089 | 2010-11-15 17:52:45 +0000 | [diff] [blame] | 440 | Value *Incoming = PI->getIncomingValue(i); |
Duncan Sands | ff10341 | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 441 | // 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] | 442 | if (Incoming == PI) continue; |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 443 | Value *V = PI == LHS ? |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 444 | SimplifyBinOp(Opcode, Incoming, RHS, TD, DT, MaxRecurse) : |
| 445 | SimplifyBinOp(Opcode, LHS, Incoming, TD, DT, MaxRecurse); |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 446 | // If the operation failed to simplify, or simplified to a different value |
| 447 | // to previously, then give up. |
| 448 | if (!V || (CommonValue && V != CommonValue)) |
| 449 | return 0; |
| 450 | CommonValue = V; |
| 451 | } |
| 452 | |
| 453 | return CommonValue; |
| 454 | } |
| 455 | |
| 456 | /// ThreadCmpOverPHI - In the case of a comparison with a PHI instruction, try |
| 457 | /// try to simplify the comparison by seeing whether comparing with all of the |
| 458 | /// incoming phi values yields the same result every time. If so returns the |
| 459 | /// common result, otherwise returns null. |
| 460 | static Value *ThreadCmpOverPHI(CmpInst::Predicate Pred, Value *LHS, Value *RHS, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 461 | const TargetData *TD, const DominatorTree *DT, |
| 462 | unsigned MaxRecurse) { |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 463 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 464 | if (!MaxRecurse--) |
| 465 | return 0; |
| 466 | |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 467 | // Make sure the phi is on the LHS. |
| 468 | if (!isa<PHINode>(LHS)) { |
| 469 | std::swap(LHS, RHS); |
| 470 | Pred = CmpInst::getSwappedPredicate(Pred); |
| 471 | } |
| 472 | assert(isa<PHINode>(LHS) && "Not comparing with a phi instruction!"); |
| 473 | PHINode *PI = cast<PHINode>(LHS); |
| 474 | |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 475 | // Bail out if RHS and the phi may be mutually interdependent due to a loop. |
| 476 | if (!ValueDominatesPHI(RHS, PI, DT)) |
| 477 | return 0; |
| 478 | |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 479 | // Evaluate the BinOp on the incoming phi values. |
| 480 | Value *CommonValue = 0; |
| 481 | for (unsigned i = 0, e = PI->getNumIncomingValues(); i != e; ++i) { |
Duncan Sands | 5520089 | 2010-11-15 17:52:45 +0000 | [diff] [blame] | 482 | Value *Incoming = PI->getIncomingValue(i); |
Duncan Sands | ff10341 | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 483 | // 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] | 484 | if (Incoming == PI) continue; |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 485 | Value *V = SimplifyCmpInst(Pred, Incoming, RHS, TD, DT, MaxRecurse); |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 486 | // If the operation failed to simplify, or simplified to a different value |
| 487 | // to previously, then give up. |
| 488 | if (!V || (CommonValue && V != CommonValue)) |
| 489 | return 0; |
| 490 | CommonValue = V; |
| 491 | } |
| 492 | |
| 493 | return CommonValue; |
| 494 | } |
| 495 | |
Chris Lattner | 8aee8ef | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 496 | /// SimplifyAddInst - Given operands for an Add, see if we can |
| 497 | /// fold the result. If not, this returns null. |
Duncan Sands | ee9a2e3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 498 | static Value *SimplifyAddInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
| 499 | const TargetData *TD, const DominatorTree *DT, |
| 500 | unsigned MaxRecurse) { |
Chris Lattner | 8aee8ef | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 501 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
| 502 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) { |
| 503 | Constant *Ops[] = { CLHS, CRHS }; |
| 504 | return ConstantFoldInstOperands(Instruction::Add, CLHS->getType(), |
| 505 | Ops, 2, TD); |
| 506 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 507 | |
Chris Lattner | 8aee8ef | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 508 | // Canonicalize the constant to the RHS. |
| 509 | std::swap(Op0, Op1); |
| 510 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 511 | |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 512 | // X + undef -> undef |
| 513 | if (isa<UndefValue>(Op1)) |
| 514 | return Op1; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 515 | |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 516 | // X + 0 -> X |
| 517 | if (match(Op1, m_Zero())) |
| 518 | return Op0; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 519 | |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 520 | // X + (Y - X) -> Y |
| 521 | // (Y - X) + X -> Y |
Duncan Sands | ee9a2e3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 522 | // Eg: X + -X -> 0 |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 523 | Value *Y = 0; |
| 524 | if (match(Op1, m_Sub(m_Value(Y), m_Specific(Op0))) || |
| 525 | match(Op0, m_Sub(m_Value(Y), m_Specific(Op1)))) |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 526 | return Y; |
| 527 | |
| 528 | // X + ~X -> -1 since ~X = -X-1 |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 529 | if (match(Op0, m_Not(m_Specific(Op1))) || |
| 530 | match(Op1, m_Not(m_Specific(Op0)))) |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 531 | return Constant::getAllOnesValue(Op0->getType()); |
Duncan Sands | 87689cf | 2010-11-19 09:20:39 +0000 | [diff] [blame] | 532 | |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 533 | /// i1 add -> xor. |
Duncan Sands | 75d289e | 2010-12-21 14:48:48 +0000 | [diff] [blame] | 534 | if (MaxRecurse && Op0->getType()->isIntegerTy(1)) |
Duncan Sands | 07f30fb | 2010-12-21 15:03:43 +0000 | [diff] [blame] | 535 | if (Value *V = SimplifyXorInst(Op0, Op1, TD, DT, MaxRecurse-1)) |
| 536 | return V; |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 537 | |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 538 | // Try some generic simplifications for associative operations. |
| 539 | if (Value *V = SimplifyAssociativeBinOp(Instruction::Add, Op0, Op1, TD, DT, |
| 540 | MaxRecurse)) |
| 541 | return V; |
| 542 | |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 543 | // Mul distributes over Add. Try some generic simplifications based on this. |
| 544 | if (Value *V = FactorizeBinOp(Instruction::Add, Op0, Op1, Instruction::Mul, |
| 545 | TD, DT, MaxRecurse)) |
| 546 | return V; |
| 547 | |
Duncan Sands | 87689cf | 2010-11-19 09:20:39 +0000 | [diff] [blame] | 548 | // Threading Add over selects and phi nodes is pointless, so don't bother. |
| 549 | // Threading over the select in "A + select(cond, B, C)" means evaluating |
| 550 | // "A+B" and "A+C" and seeing if they are equal; but they are equal if and |
| 551 | // only if B and C are equal. If B and C are equal then (since we assume |
| 552 | // that operands have already been simplified) "select(cond, B, C)" should |
| 553 | // have been simplified to the common value of B and C already. Analysing |
| 554 | // "A+B" and "A+C" thus gains nothing, but costs compile time. Similarly |
| 555 | // for threading over phi nodes. |
| 556 | |
Chris Lattner | 8aee8ef | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 557 | return 0; |
| 558 | } |
| 559 | |
Duncan Sands | ee9a2e3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 560 | Value *llvm::SimplifyAddInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
| 561 | const TargetData *TD, const DominatorTree *DT) { |
| 562 | return ::SimplifyAddInst(Op0, Op1, isNSW, isNUW, TD, DT, RecursionLimit); |
| 563 | } |
| 564 | |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 565 | /// SimplifySubInst - Given operands for a Sub, see if we can |
| 566 | /// fold the result. If not, this returns null. |
Duncan Sands | ee9a2e3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 567 | static Value *SimplifySubInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 568 | const TargetData *TD, const DominatorTree *DT, |
Duncan Sands | ee9a2e3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 569 | unsigned MaxRecurse) { |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 570 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) |
| 571 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) { |
| 572 | Constant *Ops[] = { CLHS, CRHS }; |
| 573 | return ConstantFoldInstOperands(Instruction::Sub, CLHS->getType(), |
| 574 | Ops, 2, TD); |
| 575 | } |
| 576 | |
| 577 | // X - undef -> undef |
| 578 | // undef - X -> undef |
| 579 | if (isa<UndefValue>(Op0) || isa<UndefValue>(Op1)) |
| 580 | return UndefValue::get(Op0->getType()); |
| 581 | |
| 582 | // X - 0 -> X |
| 583 | if (match(Op1, m_Zero())) |
| 584 | return Op0; |
| 585 | |
| 586 | // X - X -> 0 |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 587 | if (Op0 == Op1) |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 588 | return Constant::getNullValue(Op0->getType()); |
| 589 | |
Duncan Sands | fe02c69 | 2011-01-18 09:24:58 +0000 | [diff] [blame] | 590 | // (X*2) - X -> X |
| 591 | // (X<<1) - X -> X |
Duncan Sands | b2f3c38 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 592 | Value *X = 0; |
Duncan Sands | fe02c69 | 2011-01-18 09:24:58 +0000 | [diff] [blame] | 593 | if (match(Op0, m_Mul(m_Specific(Op1), m_ConstantInt<2>())) || |
| 594 | match(Op0, m_Shl(m_Specific(Op1), m_One()))) |
| 595 | return Op1; |
| 596 | |
Duncan Sands | b2f3c38 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 597 | // (X + Y) - Z -> X + (Y - Z) or Y + (X - Z) if everything simplifies. |
| 598 | // For example, (X + Y) - Y -> X; (Y + X) - Y -> X |
| 599 | Value *Y = 0, *Z = Op1; |
| 600 | if (MaxRecurse && match(Op0, m_Add(m_Value(X), m_Value(Y)))) { // (X + Y) - Z |
| 601 | // See if "V === Y - Z" simplifies. |
| 602 | if (Value *V = SimplifyBinOp(Instruction::Sub, Y, Z, TD, DT, MaxRecurse-1)) |
| 603 | // It does! Now see if "X + V" simplifies. |
| 604 | if (Value *W = SimplifyBinOp(Instruction::Add, X, V, TD, DT, |
| 605 | MaxRecurse-1)) { |
| 606 | // It does, we successfully reassociated! |
| 607 | ++NumReassoc; |
| 608 | return W; |
| 609 | } |
| 610 | // See if "V === X - Z" simplifies. |
| 611 | if (Value *V = SimplifyBinOp(Instruction::Sub, X, Z, TD, DT, MaxRecurse-1)) |
| 612 | // It does! Now see if "Y + V" simplifies. |
| 613 | if (Value *W = SimplifyBinOp(Instruction::Add, Y, V, TD, DT, |
| 614 | MaxRecurse-1)) { |
| 615 | // It does, we successfully reassociated! |
| 616 | ++NumReassoc; |
| 617 | return W; |
| 618 | } |
| 619 | } |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 620 | |
Duncan Sands | b2f3c38 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 621 | // X - (Y + Z) -> (X - Y) - Z or (X - Z) - Y if everything simplifies. |
| 622 | // For example, X - (X + 1) -> -1 |
| 623 | X = Op0; |
| 624 | if (MaxRecurse && match(Op1, m_Add(m_Value(Y), m_Value(Z)))) { // X - (Y + Z) |
| 625 | // See if "V === X - Y" simplifies. |
| 626 | if (Value *V = SimplifyBinOp(Instruction::Sub, X, Y, TD, DT, MaxRecurse-1)) |
| 627 | // It does! Now see if "V - Z" simplifies. |
| 628 | if (Value *W = SimplifyBinOp(Instruction::Sub, V, Z, TD, DT, |
| 629 | MaxRecurse-1)) { |
| 630 | // It does, we successfully reassociated! |
| 631 | ++NumReassoc; |
| 632 | return W; |
| 633 | } |
| 634 | // See if "V === X - Z" simplifies. |
| 635 | if (Value *V = SimplifyBinOp(Instruction::Sub, X, Z, TD, DT, MaxRecurse-1)) |
| 636 | // It does! Now see if "V - Y" simplifies. |
| 637 | if (Value *W = SimplifyBinOp(Instruction::Sub, V, Y, TD, DT, |
| 638 | MaxRecurse-1)) { |
| 639 | // It does, we successfully reassociated! |
| 640 | ++NumReassoc; |
| 641 | return W; |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | // Z - (X - Y) -> (Z - X) + Y if everything simplifies. |
| 646 | // For example, X - (X - Y) -> Y. |
| 647 | Z = Op0; |
Duncan Sands | c087e20 | 2011-01-14 15:26:10 +0000 | [diff] [blame] | 648 | if (MaxRecurse && match(Op1, m_Sub(m_Value(X), m_Value(Y)))) // Z - (X - Y) |
| 649 | // See if "V === Z - X" simplifies. |
| 650 | if (Value *V = SimplifyBinOp(Instruction::Sub, Z, X, TD, DT, MaxRecurse-1)) |
Duncan Sands | b2f3c38 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 651 | // It does! Now see if "V + Y" simplifies. |
Duncan Sands | c087e20 | 2011-01-14 15:26:10 +0000 | [diff] [blame] | 652 | if (Value *W = SimplifyBinOp(Instruction::Add, V, Y, TD, DT, |
| 653 | MaxRecurse-1)) { |
| 654 | // It does, we successfully reassociated! |
| 655 | ++NumReassoc; |
| 656 | return W; |
| 657 | } |
| 658 | |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 659 | // Mul distributes over Sub. Try some generic simplifications based on this. |
| 660 | if (Value *V = FactorizeBinOp(Instruction::Sub, Op0, Op1, Instruction::Mul, |
| 661 | TD, DT, MaxRecurse)) |
| 662 | return V; |
| 663 | |
Duncan Sands | b2f3c38 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 664 | // i1 sub -> xor. |
| 665 | if (MaxRecurse && Op0->getType()->isIntegerTy(1)) |
| 666 | if (Value *V = SimplifyXorInst(Op0, Op1, TD, DT, MaxRecurse-1)) |
| 667 | return V; |
| 668 | |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 669 | // Threading Sub over selects and phi nodes is pointless, so don't bother. |
| 670 | // Threading over the select in "A - select(cond, B, C)" means evaluating |
| 671 | // "A-B" and "A-C" and seeing if they are equal; but they are equal if and |
| 672 | // only if B and C are equal. If B and C are equal then (since we assume |
| 673 | // that operands have already been simplified) "select(cond, B, C)" should |
| 674 | // have been simplified to the common value of B and C already. Analysing |
| 675 | // "A-B" and "A-C" thus gains nothing, but costs compile time. Similarly |
| 676 | // for threading over phi nodes. |
| 677 | |
| 678 | return 0; |
| 679 | } |
| 680 | |
Duncan Sands | ee9a2e3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 681 | Value *llvm::SimplifySubInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
| 682 | const TargetData *TD, const DominatorTree *DT) { |
| 683 | return ::SimplifySubInst(Op0, Op1, isNSW, isNUW, TD, DT, RecursionLimit); |
| 684 | } |
| 685 | |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 686 | /// SimplifyMulInst - Given operands for a Mul, see if we can |
| 687 | /// fold the result. If not, this returns null. |
| 688 | static Value *SimplifyMulInst(Value *Op0, Value *Op1, const TargetData *TD, |
| 689 | const DominatorTree *DT, unsigned MaxRecurse) { |
| 690 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
| 691 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) { |
| 692 | Constant *Ops[] = { CLHS, CRHS }; |
| 693 | return ConstantFoldInstOperands(Instruction::Mul, CLHS->getType(), |
| 694 | Ops, 2, TD); |
| 695 | } |
| 696 | |
| 697 | // Canonicalize the constant to the RHS. |
| 698 | std::swap(Op0, Op1); |
| 699 | } |
| 700 | |
| 701 | // X * undef -> 0 |
| 702 | if (isa<UndefValue>(Op1)) |
| 703 | return Constant::getNullValue(Op0->getType()); |
| 704 | |
| 705 | // X * 0 -> 0 |
| 706 | if (match(Op1, m_Zero())) |
| 707 | return Op1; |
| 708 | |
| 709 | // X * 1 -> X |
| 710 | if (match(Op1, m_One())) |
| 711 | return Op0; |
| 712 | |
Duncan Sands | 1895e98 | 2011-01-30 18:03:50 +0000 | [diff] [blame] | 713 | // (X / Y) * Y -> X if the division is exact. |
| 714 | Value *X = 0, *Y = 0; |
| 715 | if ((match(Op0, m_SDiv(m_Value(X), m_Value(Y))) && Y == Op1) || // (X / Y) * Y |
| 716 | (match(Op1, m_SDiv(m_Value(X), m_Value(Y))) && Y == Op0)) { // Y * (X / Y) |
| 717 | BinaryOperator *SDiv = cast<BinaryOperator>(Y == Op1 ? Op0 : Op1); |
| 718 | if (SDiv->isExact()) |
| 719 | return X; |
| 720 | } |
| 721 | |
Nick Lewycky | 5413880 | 2011-01-29 19:55:23 +0000 | [diff] [blame] | 722 | // i1 mul -> and. |
Duncan Sands | 75d289e | 2010-12-21 14:48:48 +0000 | [diff] [blame] | 723 | if (MaxRecurse && Op0->getType()->isIntegerTy(1)) |
Duncan Sands | 07f30fb | 2010-12-21 15:03:43 +0000 | [diff] [blame] | 724 | if (Value *V = SimplifyAndInst(Op0, Op1, TD, DT, MaxRecurse-1)) |
| 725 | return V; |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 726 | |
| 727 | // Try some generic simplifications for associative operations. |
| 728 | if (Value *V = SimplifyAssociativeBinOp(Instruction::Mul, Op0, Op1, TD, DT, |
| 729 | MaxRecurse)) |
| 730 | return V; |
| 731 | |
| 732 | // Mul distributes over Add. Try some generic simplifications based on this. |
| 733 | if (Value *V = ExpandBinOp(Instruction::Mul, Op0, Op1, Instruction::Add, |
| 734 | TD, DT, MaxRecurse)) |
| 735 | return V; |
| 736 | |
| 737 | // If the operation is with the result of a select instruction, check whether |
| 738 | // operating on either branch of the select always yields the same value. |
| 739 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
| 740 | if (Value *V = ThreadBinOpOverSelect(Instruction::Mul, Op0, Op1, TD, DT, |
| 741 | MaxRecurse)) |
| 742 | return V; |
| 743 | |
| 744 | // If the operation is with the result of a phi instruction, check whether |
| 745 | // operating on all incoming values of the phi always yields the same value. |
| 746 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
| 747 | if (Value *V = ThreadBinOpOverPHI(Instruction::Mul, Op0, Op1, TD, DT, |
| 748 | MaxRecurse)) |
| 749 | return V; |
| 750 | |
| 751 | return 0; |
| 752 | } |
| 753 | |
| 754 | Value *llvm::SimplifyMulInst(Value *Op0, Value *Op1, const TargetData *TD, |
| 755 | const DominatorTree *DT) { |
| 756 | return ::SimplifyMulInst(Op0, Op1, TD, DT, RecursionLimit); |
| 757 | } |
| 758 | |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 759 | /// SimplifyDiv - Given operands for an SDiv or UDiv, see if we can |
| 760 | /// fold the result. If not, this returns null. |
| 761 | static Value *SimplifyDiv(unsigned Opcode, Value *Op0, Value *Op1, |
| 762 | const TargetData *TD, const DominatorTree *DT, |
| 763 | unsigned MaxRecurse) { |
| 764 | if (Constant *C0 = dyn_cast<Constant>(Op0)) { |
| 765 | if (Constant *C1 = dyn_cast<Constant>(Op1)) { |
| 766 | Constant *Ops[] = { C0, C1 }; |
| 767 | return ConstantFoldInstOperands(Opcode, C0->getType(), Ops, 2, TD); |
| 768 | } |
| 769 | } |
| 770 | |
Duncan Sands | a3e292c | 2011-01-28 18:50:50 +0000 | [diff] [blame] | 771 | bool isSigned = Opcode == Instruction::SDiv; |
| 772 | |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 773 | // X / undef -> undef |
| 774 | if (isa<UndefValue>(Op1)) |
| 775 | return Op1; |
| 776 | |
| 777 | // undef / X -> 0 |
| 778 | if (isa<UndefValue>(Op0)) |
| 779 | return Constant::getNullValue(Op0->getType()); |
| 780 | |
| 781 | // 0 / X -> 0, we don't need to preserve faults! |
| 782 | if (match(Op0, m_Zero())) |
| 783 | return Op0; |
| 784 | |
| 785 | // X / 1 -> X |
| 786 | if (match(Op1, m_One())) |
| 787 | return Op0; |
| 788 | // Vector case. TODO: Have m_One match vectors. |
| 789 | if (ConstantVector *Op1V = dyn_cast<ConstantVector>(Op1)) { |
| 790 | if (ConstantInt *X = cast_or_null<ConstantInt>(Op1V->getSplatValue())) |
| 791 | if (X->isOne()) |
| 792 | return Op0; |
| 793 | } |
| 794 | |
| 795 | if (Op0->getType()->isIntegerTy(1)) |
| 796 | // It can't be division by zero, hence it must be division by one. |
| 797 | return Op0; |
| 798 | |
| 799 | // X / X -> 1 |
| 800 | if (Op0 == Op1) |
| 801 | return ConstantInt::get(Op0->getType(), 1); |
| 802 | |
| 803 | // (X * Y) / Y -> X if the multiplication does not overflow. |
| 804 | Value *X = 0, *Y = 0; |
| 805 | if (match(Op0, m_Mul(m_Value(X), m_Value(Y))) && (X == Op1 || Y == Op1)) { |
| 806 | if (Y != Op1) std::swap(X, Y); // Ensure expression is (X * Y) / Y, Y = Op1 |
Duncan Sands | 7af00c0 | 2011-01-30 18:24:20 +0000 | [diff] [blame^] | 807 | // BinaryOperator *Mul = cast<BinaryOperator>(Op0); |
| 808 | // // If the Mul knows it does not overflow, then we are good to go. |
| 809 | // if ((isSigned && Mul->hasNoSignedWrap()) || |
| 810 | // (!isSigned && Mul->hasNoUnsignedWrap())) |
| 811 | // return X; |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 812 | // If X has the form X = A / Y then X * Y cannot overflow. |
| 813 | if (BinaryOperator *Div = dyn_cast<BinaryOperator>(X)) |
| 814 | if (Div->getOpcode() == Opcode && Div->getOperand(1) == Y) |
| 815 | return X; |
| 816 | } |
| 817 | |
Duncan Sands | a3e292c | 2011-01-28 18:50:50 +0000 | [diff] [blame] | 818 | // (X rem Y) / Y -> 0 |
| 819 | if ((isSigned && match(Op0, m_SRem(m_Value(), m_Specific(Op1)))) || |
| 820 | (!isSigned && match(Op0, m_URem(m_Value(), m_Specific(Op1))))) |
| 821 | return Constant::getNullValue(Op0->getType()); |
| 822 | |
| 823 | // If the operation is with the result of a select instruction, check whether |
| 824 | // operating on either branch of the select always yields the same value. |
| 825 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
| 826 | if (Value *V = ThreadBinOpOverSelect(Opcode, Op0, Op1, TD, DT, MaxRecurse)) |
| 827 | return V; |
| 828 | |
| 829 | // If the operation is with the result of a phi instruction, check whether |
| 830 | // operating on all incoming values of the phi always yields the same value. |
| 831 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
| 832 | if (Value *V = ThreadBinOpOverPHI(Opcode, Op0, Op1, TD, DT, MaxRecurse)) |
| 833 | return V; |
| 834 | |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 835 | return 0; |
| 836 | } |
| 837 | |
| 838 | /// SimplifySDivInst - Given operands for an SDiv, see if we can |
| 839 | /// fold the result. If not, this returns null. |
| 840 | static Value *SimplifySDivInst(Value *Op0, Value *Op1, const TargetData *TD, |
| 841 | const DominatorTree *DT, unsigned MaxRecurse) { |
| 842 | if (Value *V = SimplifyDiv(Instruction::SDiv, Op0, Op1, TD, DT, MaxRecurse)) |
| 843 | return V; |
| 844 | |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 845 | return 0; |
| 846 | } |
| 847 | |
| 848 | Value *llvm::SimplifySDivInst(Value *Op0, Value *Op1, const TargetData *TD, |
Frits van Bommel | 1fca2c3 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 849 | const DominatorTree *DT) { |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 850 | return ::SimplifySDivInst(Op0, Op1, TD, DT, RecursionLimit); |
| 851 | } |
| 852 | |
| 853 | /// SimplifyUDivInst - Given operands for a UDiv, see if we can |
| 854 | /// fold the result. If not, this returns null. |
| 855 | static Value *SimplifyUDivInst(Value *Op0, Value *Op1, const TargetData *TD, |
| 856 | const DominatorTree *DT, unsigned MaxRecurse) { |
| 857 | if (Value *V = SimplifyDiv(Instruction::UDiv, Op0, Op1, TD, DT, MaxRecurse)) |
| 858 | return V; |
| 859 | |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 860 | return 0; |
| 861 | } |
| 862 | |
| 863 | Value *llvm::SimplifyUDivInst(Value *Op0, Value *Op1, const TargetData *TD, |
Frits van Bommel | 1fca2c3 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 864 | const DominatorTree *DT) { |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 865 | return ::SimplifyUDivInst(Op0, Op1, TD, DT, RecursionLimit); |
| 866 | } |
| 867 | |
Frits van Bommel | 1fca2c3 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 868 | static Value *SimplifyFDivInst(Value *Op0, Value *Op1, const TargetData *TD, |
| 869 | const DominatorTree *DT, unsigned MaxRecurse) { |
| 870 | // undef / X -> undef (the undef could be a snan). |
| 871 | if (isa<UndefValue>(Op0)) |
| 872 | return Op0; |
| 873 | |
| 874 | // X / undef -> undef |
| 875 | if (isa<UndefValue>(Op1)) |
| 876 | return Op1; |
| 877 | |
| 878 | return 0; |
| 879 | } |
| 880 | |
| 881 | Value *llvm::SimplifyFDivInst(Value *Op0, Value *Op1, const TargetData *TD, |
| 882 | const DominatorTree *DT) { |
| 883 | return ::SimplifyFDivInst(Op0, Op1, TD, DT, RecursionLimit); |
| 884 | } |
| 885 | |
Duncan Sands | cf80bc1 | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 886 | /// SimplifyShift - Given operands for an Shl, LShr or AShr, see if we can |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 887 | /// fold the result. If not, this returns null. |
Duncan Sands | cf80bc1 | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 888 | static Value *SimplifyShift(unsigned Opcode, Value *Op0, Value *Op1, |
| 889 | const TargetData *TD, const DominatorTree *DT, |
| 890 | unsigned MaxRecurse) { |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 891 | if (Constant *C0 = dyn_cast<Constant>(Op0)) { |
| 892 | if (Constant *C1 = dyn_cast<Constant>(Op1)) { |
| 893 | Constant *Ops[] = { C0, C1 }; |
Duncan Sands | cf80bc1 | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 894 | return ConstantFoldInstOperands(Opcode, C0->getType(), Ops, 2, TD); |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 895 | } |
| 896 | } |
| 897 | |
Duncan Sands | cf80bc1 | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 898 | // 0 shift by X -> 0 |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 899 | if (match(Op0, m_Zero())) |
| 900 | return Op0; |
| 901 | |
Duncan Sands | cf80bc1 | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 902 | // X shift by 0 -> X |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 903 | if (match(Op1, m_Zero())) |
| 904 | return Op0; |
| 905 | |
Duncan Sands | cf80bc1 | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 906 | // X shift by undef -> undef because it may shift by the bitwidth. |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 907 | if (isa<UndefValue>(Op1)) |
| 908 | return Op1; |
| 909 | |
| 910 | // Shifting by the bitwidth or more is undefined. |
| 911 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) |
| 912 | if (CI->getValue().getLimitedValue() >= |
| 913 | Op0->getType()->getScalarSizeInBits()) |
| 914 | return UndefValue::get(Op0->getType()); |
| 915 | |
Duncan Sands | cf80bc1 | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 916 | // If the operation is with the result of a select instruction, check whether |
| 917 | // operating on either branch of the select always yields the same value. |
| 918 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
| 919 | if (Value *V = ThreadBinOpOverSelect(Opcode, Op0, Op1, TD, DT, MaxRecurse)) |
| 920 | return V; |
| 921 | |
| 922 | // If the operation is with the result of a phi instruction, check whether |
| 923 | // operating on all incoming values of the phi always yields the same value. |
| 924 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
| 925 | if (Value *V = ThreadBinOpOverPHI(Opcode, Op0, Op1, TD, DT, MaxRecurse)) |
| 926 | return V; |
| 927 | |
| 928 | return 0; |
| 929 | } |
| 930 | |
| 931 | /// SimplifyShlInst - Given operands for an Shl, see if we can |
| 932 | /// fold the result. If not, this returns null. |
| 933 | static Value *SimplifyShlInst(Value *Op0, Value *Op1, const TargetData *TD, |
| 934 | const DominatorTree *DT, unsigned MaxRecurse) { |
| 935 | if (Value *V = SimplifyShift(Instruction::Shl, Op0, Op1, TD, DT, MaxRecurse)) |
| 936 | return V; |
| 937 | |
| 938 | // undef << X -> 0 |
| 939 | if (isa<UndefValue>(Op0)) |
| 940 | return Constant::getNullValue(Op0->getType()); |
| 941 | |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 942 | return 0; |
| 943 | } |
| 944 | |
| 945 | Value *llvm::SimplifyShlInst(Value *Op0, Value *Op1, const TargetData *TD, |
| 946 | const DominatorTree *DT) { |
| 947 | return ::SimplifyShlInst(Op0, Op1, TD, DT, RecursionLimit); |
| 948 | } |
| 949 | |
| 950 | /// SimplifyLShrInst - Given operands for an LShr, see if we can |
| 951 | /// fold the result. If not, this returns null. |
| 952 | static Value *SimplifyLShrInst(Value *Op0, Value *Op1, const TargetData *TD, |
| 953 | const DominatorTree *DT, unsigned MaxRecurse) { |
Duncan Sands | cf80bc1 | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 954 | if (Value *V = SimplifyShift(Instruction::LShr, Op0, Op1, TD, DT, MaxRecurse)) |
| 955 | return V; |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 956 | |
| 957 | // undef >>l X -> 0 |
| 958 | if (isa<UndefValue>(Op0)) |
| 959 | return Constant::getNullValue(Op0->getType()); |
| 960 | |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 961 | return 0; |
| 962 | } |
| 963 | |
| 964 | Value *llvm::SimplifyLShrInst(Value *Op0, Value *Op1, const TargetData *TD, |
| 965 | const DominatorTree *DT) { |
| 966 | return ::SimplifyLShrInst(Op0, Op1, TD, DT, RecursionLimit); |
| 967 | } |
| 968 | |
| 969 | /// SimplifyAShrInst - Given operands for an AShr, see if we can |
| 970 | /// fold the result. If not, this returns null. |
| 971 | static Value *SimplifyAShrInst(Value *Op0, Value *Op1, const TargetData *TD, |
| 972 | const DominatorTree *DT, unsigned MaxRecurse) { |
Duncan Sands | cf80bc1 | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 973 | if (Value *V = SimplifyShift(Instruction::AShr, Op0, Op1, TD, DT, MaxRecurse)) |
| 974 | return V; |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 975 | |
| 976 | // all ones >>a X -> all ones |
| 977 | if (match(Op0, m_AllOnes())) |
| 978 | return Op0; |
| 979 | |
| 980 | // undef >>a X -> all ones |
| 981 | if (isa<UndefValue>(Op0)) |
| 982 | return Constant::getAllOnesValue(Op0->getType()); |
| 983 | |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 984 | return 0; |
| 985 | } |
| 986 | |
| 987 | Value *llvm::SimplifyAShrInst(Value *Op0, Value *Op1, const TargetData *TD, |
| 988 | const DominatorTree *DT) { |
| 989 | return ::SimplifyAShrInst(Op0, Op1, TD, DT, RecursionLimit); |
| 990 | } |
| 991 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 992 | /// SimplifyAndInst - Given operands for an And, see if we can |
Chris Lattner | 9f3c25a | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 993 | /// fold the result. If not, this returns null. |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 994 | static Value *SimplifyAndInst(Value *Op0, Value *Op1, const TargetData *TD, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 995 | const DominatorTree *DT, unsigned MaxRecurse) { |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 996 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
| 997 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) { |
| 998 | Constant *Ops[] = { CLHS, CRHS }; |
| 999 | return ConstantFoldInstOperands(Instruction::And, CLHS->getType(), |
| 1000 | Ops, 2, TD); |
| 1001 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1002 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1003 | // Canonicalize the constant to the RHS. |
| 1004 | std::swap(Op0, Op1); |
| 1005 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1006 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1007 | // X & undef -> 0 |
| 1008 | if (isa<UndefValue>(Op1)) |
| 1009 | return Constant::getNullValue(Op0->getType()); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1010 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1011 | // X & X = X |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1012 | if (Op0 == Op1) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1013 | return Op0; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1014 | |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1015 | // X & 0 = 0 |
| 1016 | if (match(Op1, m_Zero())) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1017 | return Op1; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1018 | |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1019 | // X & -1 = X |
| 1020 | if (match(Op1, m_AllOnes())) |
| 1021 | return Op0; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1022 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1023 | // A & ~A = ~A & A = 0 |
Chandler Carruth | e89ada9 | 2010-11-29 01:41:13 +0000 | [diff] [blame] | 1024 | Value *A = 0, *B = 0; |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1025 | if ((match(Op0, m_Not(m_Value(A))) && A == Op1) || |
| 1026 | (match(Op1, m_Not(m_Value(A))) && A == Op0)) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1027 | return Constant::getNullValue(Op0->getType()); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1028 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1029 | // (A | ?) & A = A |
| 1030 | if (match(Op0, m_Or(m_Value(A), m_Value(B))) && |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1031 | (A == Op1 || B == Op1)) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1032 | return Op1; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1033 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1034 | // A & (A | ?) = A |
| 1035 | if (match(Op1, m_Or(m_Value(A), m_Value(B))) && |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1036 | (A == Op0 || B == Op0)) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1037 | return Op0; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1038 | |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1039 | // Try some generic simplifications for associative operations. |
| 1040 | if (Value *V = SimplifyAssociativeBinOp(Instruction::And, Op0, Op1, TD, DT, |
| 1041 | MaxRecurse)) |
| 1042 | return V; |
Benjamin Kramer | 6844c8e | 2010-09-10 22:39:55 +0000 | [diff] [blame] | 1043 | |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1044 | // And distributes over Or. Try some generic simplifications based on this. |
| 1045 | if (Value *V = ExpandBinOp(Instruction::And, Op0, Op1, Instruction::Or, |
| 1046 | TD, DT, MaxRecurse)) |
| 1047 | return V; |
| 1048 | |
| 1049 | // And distributes over Xor. Try some generic simplifications based on this. |
| 1050 | if (Value *V = ExpandBinOp(Instruction::And, Op0, Op1, Instruction::Xor, |
| 1051 | TD, DT, MaxRecurse)) |
| 1052 | return V; |
| 1053 | |
| 1054 | // Or distributes over And. Try some generic simplifications based on this. |
| 1055 | if (Value *V = FactorizeBinOp(Instruction::And, Op0, Op1, Instruction::Or, |
| 1056 | TD, DT, MaxRecurse)) |
| 1057 | return V; |
| 1058 | |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 1059 | // If the operation is with the result of a select instruction, check whether |
| 1060 | // operating on either branch of the select always yields the same value. |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1061 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 1062 | if (Value *V = ThreadBinOpOverSelect(Instruction::And, Op0, Op1, TD, DT, |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1063 | MaxRecurse)) |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1064 | return V; |
| 1065 | |
| 1066 | // If the operation is with the result of a phi instruction, check whether |
| 1067 | // operating on all incoming values of the phi always yields the same value. |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1068 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 1069 | if (Value *V = ThreadBinOpOverPHI(Instruction::And, Op0, Op1, TD, DT, |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1070 | MaxRecurse)) |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 1071 | return V; |
| 1072 | |
Chris Lattner | 9f3c25a | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 1073 | return 0; |
| 1074 | } |
| 1075 | |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 1076 | Value *llvm::SimplifyAndInst(Value *Op0, Value *Op1, const TargetData *TD, |
| 1077 | const DominatorTree *DT) { |
| 1078 | return ::SimplifyAndInst(Op0, Op1, TD, DT, RecursionLimit); |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1081 | /// SimplifyOrInst - Given operands for an Or, see if we can |
| 1082 | /// fold the result. If not, this returns null. |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1083 | static Value *SimplifyOrInst(Value *Op0, Value *Op1, const TargetData *TD, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 1084 | const DominatorTree *DT, unsigned MaxRecurse) { |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1085 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
| 1086 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) { |
| 1087 | Constant *Ops[] = { CLHS, CRHS }; |
| 1088 | return ConstantFoldInstOperands(Instruction::Or, CLHS->getType(), |
| 1089 | Ops, 2, TD); |
| 1090 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1091 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1092 | // Canonicalize the constant to the RHS. |
| 1093 | std::swap(Op0, Op1); |
| 1094 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1095 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1096 | // X | undef -> -1 |
| 1097 | if (isa<UndefValue>(Op1)) |
| 1098 | return Constant::getAllOnesValue(Op0->getType()); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1099 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1100 | // X | X = X |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1101 | if (Op0 == Op1) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1102 | return Op0; |
| 1103 | |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1104 | // X | 0 = X |
| 1105 | if (match(Op1, m_Zero())) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1106 | return Op0; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1107 | |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1108 | // X | -1 = -1 |
| 1109 | if (match(Op1, m_AllOnes())) |
| 1110 | return Op1; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1111 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1112 | // A | ~A = ~A | A = -1 |
Chandler Carruth | e89ada9 | 2010-11-29 01:41:13 +0000 | [diff] [blame] | 1113 | Value *A = 0, *B = 0; |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1114 | if ((match(Op0, m_Not(m_Value(A))) && A == Op1) || |
| 1115 | (match(Op1, m_Not(m_Value(A))) && A == Op0)) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1116 | return Constant::getAllOnesValue(Op0->getType()); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1117 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1118 | // (A & ?) | A = A |
| 1119 | if (match(Op0, m_And(m_Value(A), m_Value(B))) && |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1120 | (A == Op1 || B == Op1)) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1121 | return Op1; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1122 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1123 | // A | (A & ?) = A |
| 1124 | if (match(Op1, m_And(m_Value(A), m_Value(B))) && |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1125 | (A == Op0 || B == Op0)) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1126 | return Op0; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1127 | |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1128 | // Try some generic simplifications for associative operations. |
| 1129 | if (Value *V = SimplifyAssociativeBinOp(Instruction::Or, Op0, Op1, TD, DT, |
| 1130 | MaxRecurse)) |
| 1131 | return V; |
Benjamin Kramer | 6844c8e | 2010-09-10 22:39:55 +0000 | [diff] [blame] | 1132 | |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1133 | // Or distributes over And. Try some generic simplifications based on this. |
| 1134 | if (Value *V = ExpandBinOp(Instruction::Or, Op0, Op1, Instruction::And, |
| 1135 | TD, DT, MaxRecurse)) |
| 1136 | return V; |
| 1137 | |
| 1138 | // And distributes over Or. Try some generic simplifications based on this. |
| 1139 | if (Value *V = FactorizeBinOp(Instruction::Or, Op0, Op1, Instruction::And, |
| 1140 | TD, DT, MaxRecurse)) |
| 1141 | return V; |
| 1142 | |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 1143 | // If the operation is with the result of a select instruction, check whether |
| 1144 | // operating on either branch of the select always yields the same value. |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1145 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 1146 | if (Value *V = ThreadBinOpOverSelect(Instruction::Or, Op0, Op1, TD, DT, |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1147 | MaxRecurse)) |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1148 | return V; |
| 1149 | |
| 1150 | // If the operation is with the result of a phi instruction, check whether |
| 1151 | // operating on all incoming values of the phi always yields the same value. |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1152 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 1153 | if (Value *V = ThreadBinOpOverPHI(Instruction::Or, Op0, Op1, TD, DT, |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1154 | MaxRecurse)) |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 1155 | return V; |
| 1156 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1157 | return 0; |
| 1158 | } |
| 1159 | |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 1160 | Value *llvm::SimplifyOrInst(Value *Op0, Value *Op1, const TargetData *TD, |
| 1161 | const DominatorTree *DT) { |
| 1162 | return ::SimplifyOrInst(Op0, Op1, TD, DT, RecursionLimit); |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1163 | } |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1164 | |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1165 | /// SimplifyXorInst - Given operands for a Xor, see if we can |
| 1166 | /// fold the result. If not, this returns null. |
| 1167 | static Value *SimplifyXorInst(Value *Op0, Value *Op1, const TargetData *TD, |
| 1168 | const DominatorTree *DT, unsigned MaxRecurse) { |
| 1169 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
| 1170 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) { |
| 1171 | Constant *Ops[] = { CLHS, CRHS }; |
| 1172 | return ConstantFoldInstOperands(Instruction::Xor, CLHS->getType(), |
| 1173 | Ops, 2, TD); |
| 1174 | } |
| 1175 | |
| 1176 | // Canonicalize the constant to the RHS. |
| 1177 | std::swap(Op0, Op1); |
| 1178 | } |
| 1179 | |
| 1180 | // A ^ undef -> undef |
| 1181 | if (isa<UndefValue>(Op1)) |
Duncan Sands | f8b1a5e | 2010-12-15 11:02:22 +0000 | [diff] [blame] | 1182 | return Op1; |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1183 | |
| 1184 | // A ^ 0 = A |
| 1185 | if (match(Op1, m_Zero())) |
| 1186 | return Op0; |
| 1187 | |
| 1188 | // A ^ A = 0 |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1189 | if (Op0 == Op1) |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1190 | return Constant::getNullValue(Op0->getType()); |
| 1191 | |
| 1192 | // A ^ ~A = ~A ^ A = -1 |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1193 | Value *A = 0; |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1194 | if ((match(Op0, m_Not(m_Value(A))) && A == Op1) || |
| 1195 | (match(Op1, m_Not(m_Value(A))) && A == Op0)) |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1196 | return Constant::getAllOnesValue(Op0->getType()); |
| 1197 | |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1198 | // Try some generic simplifications for associative operations. |
| 1199 | if (Value *V = SimplifyAssociativeBinOp(Instruction::Xor, Op0, Op1, TD, DT, |
| 1200 | MaxRecurse)) |
| 1201 | return V; |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1202 | |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1203 | // And distributes over Xor. Try some generic simplifications based on this. |
| 1204 | if (Value *V = FactorizeBinOp(Instruction::Xor, Op0, Op1, Instruction::And, |
| 1205 | TD, DT, MaxRecurse)) |
| 1206 | return V; |
| 1207 | |
Duncan Sands | 87689cf | 2010-11-19 09:20:39 +0000 | [diff] [blame] | 1208 | // Threading Xor over selects and phi nodes is pointless, so don't bother. |
| 1209 | // Threading over the select in "A ^ select(cond, B, C)" means evaluating |
| 1210 | // "A^B" and "A^C" and seeing if they are equal; but they are equal if and |
| 1211 | // only if B and C are equal. If B and C are equal then (since we assume |
| 1212 | // that operands have already been simplified) "select(cond, B, C)" should |
| 1213 | // have been simplified to the common value of B and C already. Analysing |
| 1214 | // "A^B" and "A^C" thus gains nothing, but costs compile time. Similarly |
| 1215 | // for threading over phi nodes. |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1216 | |
| 1217 | return 0; |
| 1218 | } |
| 1219 | |
| 1220 | Value *llvm::SimplifyXorInst(Value *Op0, Value *Op1, const TargetData *TD, |
| 1221 | const DominatorTree *DT) { |
| 1222 | return ::SimplifyXorInst(Op0, Op1, TD, DT, RecursionLimit); |
| 1223 | } |
| 1224 | |
Chris Lattner | 210c5d4 | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 1225 | static const Type *GetCompareTy(Value *Op) { |
| 1226 | return CmpInst::makeCmpResultType(Op->getType()); |
| 1227 | } |
| 1228 | |
Chris Lattner | 9dbb429 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 1229 | /// SimplifyICmpInst - Given operands for an ICmpInst, see if we can |
| 1230 | /// fold the result. If not, this returns null. |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1231 | static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 1232 | const TargetData *TD, const DominatorTree *DT, |
| 1233 | unsigned MaxRecurse) { |
Chris Lattner | 9f3c25a | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 1234 | CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate; |
Chris Lattner | 9dbb429 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 1235 | assert(CmpInst::isIntPredicate(Pred) && "Not an integer compare!"); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1236 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1237 | if (Constant *CLHS = dyn_cast<Constant>(LHS)) { |
Chris Lattner | 8f73dea | 2009-11-09 23:06:58 +0000 | [diff] [blame] | 1238 | if (Constant *CRHS = dyn_cast<Constant>(RHS)) |
| 1239 | return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, TD); |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1240 | |
| 1241 | // If we have a constant, make sure it is on the RHS. |
| 1242 | std::swap(LHS, RHS); |
| 1243 | Pred = CmpInst::getSwappedPredicate(Pred); |
| 1244 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1245 | |
Duncan Sands | 6dc9125 | 2011-01-13 08:56:29 +0000 | [diff] [blame] | 1246 | const Type *ITy = GetCompareTy(LHS); // The return type. |
| 1247 | const Type *OpTy = LHS->getType(); // The operand type. |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1248 | |
Chris Lattner | 210c5d4 | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 1249 | // icmp X, X -> true/false |
Chris Lattner | c8e14b3 | 2010-03-03 19:46:03 +0000 | [diff] [blame] | 1250 | // X icmp undef -> true/false. For example, icmp ugt %X, undef -> false |
| 1251 | // because X could be 0. |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1252 | if (LHS == RHS || isa<UndefValue>(RHS)) |
Chris Lattner | 210c5d4 | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 1253 | return ConstantInt::get(ITy, CmpInst::isTrueWhenEqual(Pred)); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1254 | |
Duncan Sands | 6dc9125 | 2011-01-13 08:56:29 +0000 | [diff] [blame] | 1255 | // Special case logic when the operands have i1 type. |
| 1256 | if (OpTy->isIntegerTy(1) || (OpTy->isVectorTy() && |
| 1257 | cast<VectorType>(OpTy)->getElementType()->isIntegerTy(1))) { |
| 1258 | switch (Pred) { |
| 1259 | default: break; |
| 1260 | case ICmpInst::ICMP_EQ: |
| 1261 | // X == 1 -> X |
| 1262 | if (match(RHS, m_One())) |
| 1263 | return LHS; |
| 1264 | break; |
| 1265 | case ICmpInst::ICMP_NE: |
| 1266 | // X != 0 -> X |
| 1267 | if (match(RHS, m_Zero())) |
| 1268 | return LHS; |
| 1269 | break; |
| 1270 | case ICmpInst::ICMP_UGT: |
| 1271 | // X >u 0 -> X |
| 1272 | if (match(RHS, m_Zero())) |
| 1273 | return LHS; |
| 1274 | break; |
| 1275 | case ICmpInst::ICMP_UGE: |
| 1276 | // X >=u 1 -> X |
| 1277 | if (match(RHS, m_One())) |
| 1278 | return LHS; |
| 1279 | break; |
| 1280 | case ICmpInst::ICMP_SLT: |
| 1281 | // X <s 0 -> X |
| 1282 | if (match(RHS, m_Zero())) |
| 1283 | return LHS; |
| 1284 | break; |
| 1285 | case ICmpInst::ICMP_SLE: |
| 1286 | // X <=s -1 -> X |
| 1287 | if (match(RHS, m_One())) |
| 1288 | return LHS; |
| 1289 | break; |
| 1290 | } |
| 1291 | } |
| 1292 | |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1293 | // icmp <alloca*>, <global/alloca*/null> - Different stack variables have |
| 1294 | // different addresses, and what's more the address of a stack variable is |
| 1295 | // never null or equal to the address of a global. Note that generalizing |
| 1296 | // to the case where LHS is a global variable address or null is pointless, |
| 1297 | // since if both LHS and RHS are constants then we already constant folded |
| 1298 | // the compare, and if only one of them is then we moved it to RHS already. |
| 1299 | if (isa<AllocaInst>(LHS) && (isa<GlobalValue>(RHS) || isa<AllocaInst>(RHS) || |
| 1300 | isa<ConstantPointerNull>(RHS))) |
| 1301 | // We already know that LHS != LHS. |
| 1302 | return ConstantInt::get(ITy, CmpInst::isFalseWhenEqual(Pred)); |
| 1303 | |
| 1304 | // If we are comparing with zero then try hard since this is a common case. |
| 1305 | if (match(RHS, m_Zero())) { |
| 1306 | bool LHSKnownNonNegative, LHSKnownNegative; |
| 1307 | switch (Pred) { |
| 1308 | default: |
| 1309 | assert(false && "Unknown ICmp predicate!"); |
| 1310 | case ICmpInst::ICMP_ULT: |
| 1311 | return ConstantInt::getFalse(LHS->getContext()); |
| 1312 | case ICmpInst::ICMP_UGE: |
| 1313 | return ConstantInt::getTrue(LHS->getContext()); |
| 1314 | case ICmpInst::ICMP_EQ: |
| 1315 | case ICmpInst::ICMP_ULE: |
| 1316 | if (isKnownNonZero(LHS, TD)) |
| 1317 | return ConstantInt::getFalse(LHS->getContext()); |
| 1318 | break; |
| 1319 | case ICmpInst::ICMP_NE: |
| 1320 | case ICmpInst::ICMP_UGT: |
| 1321 | if (isKnownNonZero(LHS, TD)) |
| 1322 | return ConstantInt::getTrue(LHS->getContext()); |
| 1323 | break; |
| 1324 | case ICmpInst::ICMP_SLT: |
| 1325 | ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, TD); |
| 1326 | if (LHSKnownNegative) |
| 1327 | return ConstantInt::getTrue(LHS->getContext()); |
| 1328 | if (LHSKnownNonNegative) |
| 1329 | return ConstantInt::getFalse(LHS->getContext()); |
| 1330 | break; |
| 1331 | case ICmpInst::ICMP_SLE: |
| 1332 | ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, TD); |
| 1333 | if (LHSKnownNegative) |
| 1334 | return ConstantInt::getTrue(LHS->getContext()); |
| 1335 | if (LHSKnownNonNegative && isKnownNonZero(LHS, TD)) |
| 1336 | return ConstantInt::getFalse(LHS->getContext()); |
| 1337 | break; |
| 1338 | case ICmpInst::ICMP_SGE: |
| 1339 | ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, TD); |
| 1340 | if (LHSKnownNegative) |
| 1341 | return ConstantInt::getFalse(LHS->getContext()); |
| 1342 | if (LHSKnownNonNegative) |
| 1343 | return ConstantInt::getTrue(LHS->getContext()); |
| 1344 | break; |
| 1345 | case ICmpInst::ICMP_SGT: |
| 1346 | ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, TD); |
| 1347 | if (LHSKnownNegative) |
| 1348 | return ConstantInt::getFalse(LHS->getContext()); |
| 1349 | if (LHSKnownNonNegative && isKnownNonZero(LHS, TD)) |
| 1350 | return ConstantInt::getTrue(LHS->getContext()); |
| 1351 | break; |
| 1352 | } |
| 1353 | } |
| 1354 | |
| 1355 | // See if we are doing a comparison with a constant integer. |
Duncan Sands | 6dc9125 | 2011-01-13 08:56:29 +0000 | [diff] [blame] | 1356 | if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) { |
| 1357 | switch (Pred) { |
| 1358 | default: break; |
| 1359 | case ICmpInst::ICMP_UGT: |
| 1360 | if (CI->isMaxValue(false)) // A >u MAX -> FALSE |
| 1361 | return ConstantInt::getFalse(CI->getContext()); |
| 1362 | break; |
| 1363 | case ICmpInst::ICMP_UGE: |
| 1364 | if (CI->isMinValue(false)) // A >=u MIN -> TRUE |
| 1365 | return ConstantInt::getTrue(CI->getContext()); |
| 1366 | break; |
| 1367 | case ICmpInst::ICMP_ULT: |
| 1368 | if (CI->isMinValue(false)) // A <u MIN -> FALSE |
| 1369 | return ConstantInt::getFalse(CI->getContext()); |
| 1370 | break; |
| 1371 | case ICmpInst::ICMP_ULE: |
| 1372 | if (CI->isMaxValue(false)) // A <=u MAX -> TRUE |
| 1373 | return ConstantInt::getTrue(CI->getContext()); |
| 1374 | break; |
| 1375 | case ICmpInst::ICMP_SGT: |
| 1376 | if (CI->isMaxValue(true)) // A >s MAX -> FALSE |
| 1377 | return ConstantInt::getFalse(CI->getContext()); |
| 1378 | break; |
| 1379 | case ICmpInst::ICMP_SGE: |
| 1380 | if (CI->isMinValue(true)) // A >=s MIN -> TRUE |
| 1381 | return ConstantInt::getTrue(CI->getContext()); |
| 1382 | break; |
| 1383 | case ICmpInst::ICMP_SLT: |
| 1384 | if (CI->isMinValue(true)) // A <s MIN -> FALSE |
| 1385 | return ConstantInt::getFalse(CI->getContext()); |
| 1386 | break; |
| 1387 | case ICmpInst::ICMP_SLE: |
| 1388 | if (CI->isMaxValue(true)) // A <=s MAX -> TRUE |
| 1389 | return ConstantInt::getTrue(CI->getContext()); |
| 1390 | break; |
| 1391 | } |
| 1392 | } |
| 1393 | |
Duncan Sands | 9d32f60 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 1394 | // Compare of cast, for example (zext X) != 0 -> X != 0 |
| 1395 | if (isa<CastInst>(LHS) && (isa<Constant>(RHS) || isa<CastInst>(RHS))) { |
| 1396 | Instruction *LI = cast<CastInst>(LHS); |
| 1397 | Value *SrcOp = LI->getOperand(0); |
| 1398 | const Type *SrcTy = SrcOp->getType(); |
| 1399 | const Type *DstTy = LI->getType(); |
| 1400 | |
| 1401 | // Turn icmp (ptrtoint x), (ptrtoint/constant) into a compare of the input |
| 1402 | // if the integer type is the same size as the pointer type. |
| 1403 | if (MaxRecurse && TD && isa<PtrToIntInst>(LI) && |
| 1404 | TD->getPointerSizeInBits() == DstTy->getPrimitiveSizeInBits()) { |
| 1405 | if (Constant *RHSC = dyn_cast<Constant>(RHS)) { |
| 1406 | // Transfer the cast to the constant. |
| 1407 | if (Value *V = SimplifyICmpInst(Pred, SrcOp, |
| 1408 | ConstantExpr::getIntToPtr(RHSC, SrcTy), |
| 1409 | TD, DT, MaxRecurse-1)) |
| 1410 | return V; |
| 1411 | } else if (PtrToIntInst *RI = dyn_cast<PtrToIntInst>(RHS)) { |
| 1412 | if (RI->getOperand(0)->getType() == SrcTy) |
| 1413 | // Compare without the cast. |
| 1414 | if (Value *V = SimplifyICmpInst(Pred, SrcOp, RI->getOperand(0), |
| 1415 | TD, DT, MaxRecurse-1)) |
| 1416 | return V; |
| 1417 | } |
| 1418 | } |
| 1419 | |
| 1420 | if (isa<ZExtInst>(LHS)) { |
| 1421 | // Turn icmp (zext X), (zext Y) into a compare of X and Y if they have the |
| 1422 | // same type. |
| 1423 | if (ZExtInst *RI = dyn_cast<ZExtInst>(RHS)) { |
| 1424 | if (MaxRecurse && SrcTy == RI->getOperand(0)->getType()) |
| 1425 | // Compare X and Y. Note that signed predicates become unsigned. |
| 1426 | if (Value *V = SimplifyICmpInst(ICmpInst::getUnsignedPredicate(Pred), |
| 1427 | SrcOp, RI->getOperand(0), TD, DT, |
| 1428 | MaxRecurse-1)) |
| 1429 | return V; |
| 1430 | } |
| 1431 | // Turn icmp (zext X), Cst into a compare of X and Cst if Cst is extended |
| 1432 | // too. If not, then try to deduce the result of the comparison. |
| 1433 | else if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) { |
| 1434 | // Compute the constant that would happen if we truncated to SrcTy then |
| 1435 | // reextended to DstTy. |
| 1436 | Constant *Trunc = ConstantExpr::getTrunc(CI, SrcTy); |
| 1437 | Constant *RExt = ConstantExpr::getCast(CastInst::ZExt, Trunc, DstTy); |
| 1438 | |
| 1439 | // If the re-extended constant didn't change then this is effectively |
| 1440 | // also a case of comparing two zero-extended values. |
| 1441 | if (RExt == CI && MaxRecurse) |
| 1442 | if (Value *V = SimplifyICmpInst(ICmpInst::getUnsignedPredicate(Pred), |
| 1443 | SrcOp, Trunc, TD, DT, MaxRecurse-1)) |
| 1444 | return V; |
| 1445 | |
| 1446 | // Otherwise the upper bits of LHS are zero while RHS has a non-zero bit |
| 1447 | // there. Use this to work out the result of the comparison. |
| 1448 | if (RExt != CI) { |
| 1449 | switch (Pred) { |
| 1450 | default: |
| 1451 | assert(false && "Unknown ICmp predicate!"); |
| 1452 | // LHS <u RHS. |
| 1453 | case ICmpInst::ICMP_EQ: |
| 1454 | case ICmpInst::ICMP_UGT: |
| 1455 | case ICmpInst::ICMP_UGE: |
| 1456 | return ConstantInt::getFalse(CI->getContext()); |
| 1457 | |
| 1458 | case ICmpInst::ICMP_NE: |
| 1459 | case ICmpInst::ICMP_ULT: |
| 1460 | case ICmpInst::ICMP_ULE: |
| 1461 | return ConstantInt::getTrue(CI->getContext()); |
| 1462 | |
| 1463 | // LHS is non-negative. If RHS is negative then LHS >s LHS. If RHS |
| 1464 | // is non-negative then LHS <s RHS. |
| 1465 | case ICmpInst::ICMP_SGT: |
| 1466 | case ICmpInst::ICMP_SGE: |
| 1467 | return CI->getValue().isNegative() ? |
| 1468 | ConstantInt::getTrue(CI->getContext()) : |
| 1469 | ConstantInt::getFalse(CI->getContext()); |
| 1470 | |
| 1471 | case ICmpInst::ICMP_SLT: |
| 1472 | case ICmpInst::ICMP_SLE: |
| 1473 | return CI->getValue().isNegative() ? |
| 1474 | ConstantInt::getFalse(CI->getContext()) : |
| 1475 | ConstantInt::getTrue(CI->getContext()); |
| 1476 | } |
| 1477 | } |
| 1478 | } |
| 1479 | } |
| 1480 | |
| 1481 | if (isa<SExtInst>(LHS)) { |
| 1482 | // Turn icmp (sext X), (sext Y) into a compare of X and Y if they have the |
| 1483 | // same type. |
| 1484 | if (SExtInst *RI = dyn_cast<SExtInst>(RHS)) { |
| 1485 | if (MaxRecurse && SrcTy == RI->getOperand(0)->getType()) |
| 1486 | // Compare X and Y. Note that the predicate does not change. |
| 1487 | if (Value *V = SimplifyICmpInst(Pred, SrcOp, RI->getOperand(0), |
| 1488 | TD, DT, MaxRecurse-1)) |
| 1489 | return V; |
| 1490 | } |
| 1491 | // Turn icmp (sext X), Cst into a compare of X and Cst if Cst is extended |
| 1492 | // too. If not, then try to deduce the result of the comparison. |
| 1493 | else if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) { |
| 1494 | // Compute the constant that would happen if we truncated to SrcTy then |
| 1495 | // reextended to DstTy. |
| 1496 | Constant *Trunc = ConstantExpr::getTrunc(CI, SrcTy); |
| 1497 | Constant *RExt = ConstantExpr::getCast(CastInst::SExt, Trunc, DstTy); |
| 1498 | |
| 1499 | // If the re-extended constant didn't change then this is effectively |
| 1500 | // also a case of comparing two sign-extended values. |
| 1501 | if (RExt == CI && MaxRecurse) |
| 1502 | if (Value *V = SimplifyICmpInst(Pred, SrcOp, Trunc, TD, DT, |
| 1503 | MaxRecurse-1)) |
| 1504 | return V; |
| 1505 | |
| 1506 | // Otherwise the upper bits of LHS are all equal, while RHS has varying |
| 1507 | // bits there. Use this to work out the result of the comparison. |
| 1508 | if (RExt != CI) { |
| 1509 | switch (Pred) { |
| 1510 | default: |
| 1511 | assert(false && "Unknown ICmp predicate!"); |
| 1512 | case ICmpInst::ICMP_EQ: |
| 1513 | return ConstantInt::getFalse(CI->getContext()); |
| 1514 | case ICmpInst::ICMP_NE: |
| 1515 | return ConstantInt::getTrue(CI->getContext()); |
| 1516 | |
| 1517 | // If RHS is non-negative then LHS <s RHS. If RHS is negative then |
| 1518 | // LHS >s RHS. |
| 1519 | case ICmpInst::ICMP_SGT: |
| 1520 | case ICmpInst::ICMP_SGE: |
| 1521 | return CI->getValue().isNegative() ? |
| 1522 | ConstantInt::getTrue(CI->getContext()) : |
| 1523 | ConstantInt::getFalse(CI->getContext()); |
| 1524 | case ICmpInst::ICMP_SLT: |
| 1525 | case ICmpInst::ICMP_SLE: |
| 1526 | return CI->getValue().isNegative() ? |
| 1527 | ConstantInt::getFalse(CI->getContext()) : |
| 1528 | ConstantInt::getTrue(CI->getContext()); |
| 1529 | |
| 1530 | // If LHS is non-negative then LHS <u RHS. If LHS is negative then |
| 1531 | // LHS >u RHS. |
| 1532 | case ICmpInst::ICMP_UGT: |
| 1533 | case ICmpInst::ICMP_UGE: |
| 1534 | // Comparison is true iff the LHS <s 0. |
| 1535 | if (MaxRecurse) |
| 1536 | if (Value *V = SimplifyICmpInst(ICmpInst::ICMP_SLT, SrcOp, |
| 1537 | Constant::getNullValue(SrcTy), |
| 1538 | TD, DT, MaxRecurse-1)) |
| 1539 | return V; |
| 1540 | break; |
| 1541 | case ICmpInst::ICMP_ULT: |
| 1542 | case ICmpInst::ICMP_ULE: |
| 1543 | // Comparison is true iff the LHS >=s 0. |
| 1544 | if (MaxRecurse) |
| 1545 | if (Value *V = SimplifyICmpInst(ICmpInst::ICMP_SGE, SrcOp, |
| 1546 | Constant::getNullValue(SrcTy), |
| 1547 | TD, DT, MaxRecurse-1)) |
| 1548 | return V; |
| 1549 | break; |
| 1550 | } |
| 1551 | } |
| 1552 | } |
| 1553 | } |
| 1554 | } |
| 1555 | |
Duncan Sands | 1ac7c99 | 2010-11-07 16:12:23 +0000 | [diff] [blame] | 1556 | // If the comparison is with the result of a select instruction, check whether |
| 1557 | // comparing with either branch of the select always yields the same value. |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1558 | if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS)) |
| 1559 | if (Value *V = ThreadCmpOverSelect(Pred, LHS, RHS, TD, DT, MaxRecurse)) |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1560 | return V; |
| 1561 | |
| 1562 | // If the comparison is with the result of a phi instruction, check whether |
| 1563 | // doing the compare with each incoming phi value yields a common result. |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1564 | if (isa<PHINode>(LHS) || isa<PHINode>(RHS)) |
| 1565 | if (Value *V = ThreadCmpOverPHI(Pred, LHS, RHS, TD, DT, MaxRecurse)) |
Duncan Sands | 3bbb0cc | 2010-11-09 17:25:51 +0000 | [diff] [blame] | 1566 | return V; |
Duncan Sands | 1ac7c99 | 2010-11-07 16:12:23 +0000 | [diff] [blame] | 1567 | |
Chris Lattner | 9f3c25a | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 1568 | return 0; |
| 1569 | } |
| 1570 | |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1571 | Value *llvm::SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 1572 | const TargetData *TD, const DominatorTree *DT) { |
| 1573 | return ::SimplifyICmpInst(Predicate, LHS, RHS, TD, DT, RecursionLimit); |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1574 | } |
| 1575 | |
Chris Lattner | 9dbb429 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 1576 | /// SimplifyFCmpInst - Given operands for an FCmpInst, see if we can |
| 1577 | /// fold the result. If not, this returns null. |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1578 | static Value *SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 1579 | const TargetData *TD, const DominatorTree *DT, |
| 1580 | unsigned MaxRecurse) { |
Chris Lattner | 9dbb429 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 1581 | CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate; |
| 1582 | assert(CmpInst::isFPPredicate(Pred) && "Not an FP compare!"); |
| 1583 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1584 | if (Constant *CLHS = dyn_cast<Constant>(LHS)) { |
Chris Lattner | 9dbb429 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 1585 | if (Constant *CRHS = dyn_cast<Constant>(RHS)) |
| 1586 | return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, TD); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1587 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1588 | // If we have a constant, make sure it is on the RHS. |
| 1589 | std::swap(LHS, RHS); |
| 1590 | Pred = CmpInst::getSwappedPredicate(Pred); |
| 1591 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1592 | |
Chris Lattner | 210c5d4 | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 1593 | // Fold trivial predicates. |
| 1594 | if (Pred == FCmpInst::FCMP_FALSE) |
| 1595 | return ConstantInt::get(GetCompareTy(LHS), 0); |
| 1596 | if (Pred == FCmpInst::FCMP_TRUE) |
| 1597 | return ConstantInt::get(GetCompareTy(LHS), 1); |
| 1598 | |
Chris Lattner | 210c5d4 | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 1599 | if (isa<UndefValue>(RHS)) // fcmp pred X, undef -> undef |
| 1600 | return UndefValue::get(GetCompareTy(LHS)); |
| 1601 | |
| 1602 | // fcmp x,x -> true/false. Not all compares are foldable. |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1603 | if (LHS == RHS) { |
Chris Lattner | 210c5d4 | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 1604 | if (CmpInst::isTrueWhenEqual(Pred)) |
| 1605 | return ConstantInt::get(GetCompareTy(LHS), 1); |
| 1606 | if (CmpInst::isFalseWhenEqual(Pred)) |
| 1607 | return ConstantInt::get(GetCompareTy(LHS), 0); |
| 1608 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1609 | |
Chris Lattner | 210c5d4 | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 1610 | // Handle fcmp with constant RHS |
| 1611 | if (Constant *RHSC = dyn_cast<Constant>(RHS)) { |
| 1612 | // If the constant is a nan, see if we can fold the comparison based on it. |
| 1613 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) { |
| 1614 | if (CFP->getValueAPF().isNaN()) { |
| 1615 | if (FCmpInst::isOrdered(Pred)) // True "if ordered and foo" |
| 1616 | return ConstantInt::getFalse(CFP->getContext()); |
| 1617 | assert(FCmpInst::isUnordered(Pred) && |
| 1618 | "Comparison must be either ordered or unordered!"); |
| 1619 | // True if unordered. |
| 1620 | return ConstantInt::getTrue(CFP->getContext()); |
| 1621 | } |
Dan Gohman | 6b617a7 | 2010-02-22 04:06:03 +0000 | [diff] [blame] | 1622 | // Check whether the constant is an infinity. |
| 1623 | if (CFP->getValueAPF().isInfinity()) { |
| 1624 | if (CFP->getValueAPF().isNegative()) { |
| 1625 | switch (Pred) { |
| 1626 | case FCmpInst::FCMP_OLT: |
| 1627 | // No value is ordered and less than negative infinity. |
| 1628 | return ConstantInt::getFalse(CFP->getContext()); |
| 1629 | case FCmpInst::FCMP_UGE: |
| 1630 | // All values are unordered with or at least negative infinity. |
| 1631 | return ConstantInt::getTrue(CFP->getContext()); |
| 1632 | default: |
| 1633 | break; |
| 1634 | } |
| 1635 | } else { |
| 1636 | switch (Pred) { |
| 1637 | case FCmpInst::FCMP_OGT: |
| 1638 | // No value is ordered and greater than infinity. |
| 1639 | return ConstantInt::getFalse(CFP->getContext()); |
| 1640 | case FCmpInst::FCMP_ULE: |
| 1641 | // All values are unordered with and at most infinity. |
| 1642 | return ConstantInt::getTrue(CFP->getContext()); |
| 1643 | default: |
| 1644 | break; |
| 1645 | } |
| 1646 | } |
| 1647 | } |
Chris Lattner | 210c5d4 | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 1648 | } |
| 1649 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1650 | |
Duncan Sands | 92826de | 2010-11-07 16:46:25 +0000 | [diff] [blame] | 1651 | // If the comparison is with the result of a select instruction, check whether |
| 1652 | // comparing with either branch of the select always yields the same value. |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1653 | if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS)) |
| 1654 | if (Value *V = ThreadCmpOverSelect(Pred, LHS, RHS, TD, DT, MaxRecurse)) |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1655 | return V; |
| 1656 | |
| 1657 | // If the comparison is with the result of a phi instruction, check whether |
| 1658 | // doing the compare with each incoming phi value yields a common result. |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1659 | if (isa<PHINode>(LHS) || isa<PHINode>(RHS)) |
| 1660 | if (Value *V = ThreadCmpOverPHI(Pred, LHS, RHS, TD, DT, MaxRecurse)) |
Duncan Sands | 3bbb0cc | 2010-11-09 17:25:51 +0000 | [diff] [blame] | 1661 | return V; |
Duncan Sands | 92826de | 2010-11-07 16:46:25 +0000 | [diff] [blame] | 1662 | |
Chris Lattner | 9dbb429 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 1663 | return 0; |
| 1664 | } |
| 1665 | |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1666 | Value *llvm::SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 1667 | const TargetData *TD, const DominatorTree *DT) { |
| 1668 | return ::SimplifyFCmpInst(Predicate, LHS, RHS, TD, DT, RecursionLimit); |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1669 | } |
| 1670 | |
Chris Lattner | 0475426 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 1671 | /// SimplifySelectInst - Given operands for a SelectInst, see if we can fold |
| 1672 | /// the result. If not, this returns null. |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1673 | Value *llvm::SimplifySelectInst(Value *CondVal, Value *TrueVal, Value *FalseVal, |
| 1674 | const TargetData *TD, const DominatorTree *) { |
Chris Lattner | 0475426 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 1675 | // select true, X, Y -> X |
| 1676 | // select false, X, Y -> Y |
| 1677 | if (ConstantInt *CB = dyn_cast<ConstantInt>(CondVal)) |
| 1678 | return CB->getZExtValue() ? TrueVal : FalseVal; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1679 | |
Chris Lattner | 0475426 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 1680 | // select C, X, X -> X |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1681 | if (TrueVal == FalseVal) |
Chris Lattner | 0475426 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 1682 | return TrueVal; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1683 | |
Chris Lattner | 0475426 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 1684 | if (isa<UndefValue>(TrueVal)) // select C, undef, X -> X |
| 1685 | return FalseVal; |
| 1686 | if (isa<UndefValue>(FalseVal)) // select C, X, undef -> X |
| 1687 | return TrueVal; |
| 1688 | if (isa<UndefValue>(CondVal)) { // select undef, X, Y -> X or Y |
| 1689 | if (isa<Constant>(TrueVal)) |
| 1690 | return TrueVal; |
| 1691 | return FalseVal; |
| 1692 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1693 | |
Chris Lattner | 0475426 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 1694 | return 0; |
| 1695 | } |
| 1696 | |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 1697 | /// SimplifyGEPInst - Given operands for an GetElementPtrInst, see if we can |
| 1698 | /// fold the result. If not, this returns null. |
| 1699 | Value *llvm::SimplifyGEPInst(Value *const *Ops, unsigned NumOps, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 1700 | const TargetData *TD, const DominatorTree *) { |
Duncan Sands | 85bbff6 | 2010-11-22 13:42:49 +0000 | [diff] [blame] | 1701 | // The type of the GEP pointer operand. |
| 1702 | const PointerType *PtrTy = cast<PointerType>(Ops[0]->getType()); |
| 1703 | |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 1704 | // getelementptr P -> P. |
| 1705 | if (NumOps == 1) |
| 1706 | return Ops[0]; |
| 1707 | |
Duncan Sands | 85bbff6 | 2010-11-22 13:42:49 +0000 | [diff] [blame] | 1708 | if (isa<UndefValue>(Ops[0])) { |
| 1709 | // Compute the (pointer) type returned by the GEP instruction. |
| 1710 | const Type *LastType = GetElementPtrInst::getIndexedType(PtrTy, &Ops[1], |
| 1711 | NumOps-1); |
| 1712 | const Type *GEPTy = PointerType::get(LastType, PtrTy->getAddressSpace()); |
| 1713 | return UndefValue::get(GEPTy); |
| 1714 | } |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 1715 | |
Duncan Sands | e60d79f | 2010-11-21 13:53:09 +0000 | [diff] [blame] | 1716 | if (NumOps == 2) { |
| 1717 | // getelementptr P, 0 -> P. |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 1718 | if (ConstantInt *C = dyn_cast<ConstantInt>(Ops[1])) |
| 1719 | if (C->isZero()) |
| 1720 | return Ops[0]; |
Duncan Sands | e60d79f | 2010-11-21 13:53:09 +0000 | [diff] [blame] | 1721 | // getelementptr P, N -> P if P points to a type of zero size. |
| 1722 | if (TD) { |
Duncan Sands | 85bbff6 | 2010-11-22 13:42:49 +0000 | [diff] [blame] | 1723 | const Type *Ty = PtrTy->getElementType(); |
Duncan Sands | a63395a | 2010-11-22 16:32:50 +0000 | [diff] [blame] | 1724 | if (Ty->isSized() && TD->getTypeAllocSize(Ty) == 0) |
Duncan Sands | e60d79f | 2010-11-21 13:53:09 +0000 | [diff] [blame] | 1725 | return Ops[0]; |
| 1726 | } |
| 1727 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1728 | |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 1729 | // Check to see if this is constant foldable. |
| 1730 | for (unsigned i = 0; i != NumOps; ++i) |
| 1731 | if (!isa<Constant>(Ops[i])) |
| 1732 | return 0; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1733 | |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 1734 | return ConstantExpr::getGetElementPtr(cast<Constant>(Ops[0]), |
| 1735 | (Constant *const*)Ops+1, NumOps-1); |
| 1736 | } |
| 1737 | |
Duncan Sands | ff10341 | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 1738 | /// SimplifyPHINode - See if we can fold the given phi. If not, returns null. |
| 1739 | static Value *SimplifyPHINode(PHINode *PN, const DominatorTree *DT) { |
| 1740 | // If all of the PHI's incoming values are the same then replace the PHI node |
| 1741 | // with the common value. |
| 1742 | Value *CommonValue = 0; |
| 1743 | bool HasUndefInput = false; |
| 1744 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 1745 | Value *Incoming = PN->getIncomingValue(i); |
| 1746 | // If the incoming value is the phi node itself, it can safely be skipped. |
| 1747 | if (Incoming == PN) continue; |
| 1748 | if (isa<UndefValue>(Incoming)) { |
| 1749 | // Remember that we saw an undef value, but otherwise ignore them. |
| 1750 | HasUndefInput = true; |
| 1751 | continue; |
| 1752 | } |
| 1753 | if (CommonValue && Incoming != CommonValue) |
| 1754 | return 0; // Not the same, bail out. |
| 1755 | CommonValue = Incoming; |
| 1756 | } |
| 1757 | |
| 1758 | // If CommonValue is null then all of the incoming values were either undef or |
| 1759 | // equal to the phi node itself. |
| 1760 | if (!CommonValue) |
| 1761 | return UndefValue::get(PN->getType()); |
| 1762 | |
| 1763 | // If we have a PHI node like phi(X, undef, X), where X is defined by some |
| 1764 | // instruction, we cannot return X as the result of the PHI node unless it |
| 1765 | // dominates the PHI block. |
| 1766 | if (HasUndefInput) |
| 1767 | return ValueDominatesPHI(CommonValue, PN, DT) ? CommonValue : 0; |
| 1768 | |
| 1769 | return CommonValue; |
| 1770 | } |
| 1771 | |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 1772 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1773 | //=== Helper functions for higher up the class hierarchy. |
Chris Lattner | 9dbb429 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 1774 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1775 | /// SimplifyBinOp - Given operands for a BinaryOperator, see if we can |
| 1776 | /// fold the result. If not, this returns null. |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1777 | static Value *SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 1778 | const TargetData *TD, const DominatorTree *DT, |
| 1779 | unsigned MaxRecurse) { |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1780 | switch (Opcode) { |
Duncan Sands | ee9a2e3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 1781 | case Instruction::Add: return SimplifyAddInst(LHS, RHS, /* isNSW */ false, |
| 1782 | /* isNUW */ false, TD, DT, |
| 1783 | MaxRecurse); |
| 1784 | case Instruction::Sub: return SimplifySubInst(LHS, RHS, /* isNSW */ false, |
| 1785 | /* isNUW */ false, TD, DT, |
| 1786 | MaxRecurse); |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 1787 | case Instruction::Mul: return SimplifyMulInst(LHS, RHS, TD, DT, MaxRecurse); |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1788 | case Instruction::SDiv: return SimplifySDivInst(LHS, RHS, TD, DT, MaxRecurse); |
| 1789 | case Instruction::UDiv: return SimplifyUDivInst(LHS, RHS, TD, DT, MaxRecurse); |
Frits van Bommel | 1fca2c3 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 1790 | case Instruction::FDiv: return SimplifyFDivInst(LHS, RHS, TD, DT, MaxRecurse); |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1791 | case Instruction::Shl: return SimplifyShlInst(LHS, RHS, TD, DT, MaxRecurse); |
| 1792 | case Instruction::LShr: return SimplifyLShrInst(LHS, RHS, TD, DT, MaxRecurse); |
| 1793 | case Instruction::AShr: return SimplifyAShrInst(LHS, RHS, TD, DT, MaxRecurse); |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 1794 | case Instruction::And: return SimplifyAndInst(LHS, RHS, TD, DT, MaxRecurse); |
| 1795 | case Instruction::Or: return SimplifyOrInst(LHS, RHS, TD, DT, MaxRecurse); |
| 1796 | case Instruction::Xor: return SimplifyXorInst(LHS, RHS, TD, DT, MaxRecurse); |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1797 | default: |
| 1798 | if (Constant *CLHS = dyn_cast<Constant>(LHS)) |
| 1799 | if (Constant *CRHS = dyn_cast<Constant>(RHS)) { |
| 1800 | Constant *COps[] = {CLHS, CRHS}; |
| 1801 | return ConstantFoldInstOperands(Opcode, LHS->getType(), COps, 2, TD); |
| 1802 | } |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 1803 | |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1804 | // If the operation is associative, try some generic simplifications. |
| 1805 | if (Instruction::isAssociative(Opcode)) |
| 1806 | if (Value *V = SimplifyAssociativeBinOp(Opcode, LHS, RHS, TD, DT, |
| 1807 | MaxRecurse)) |
| 1808 | return V; |
| 1809 | |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 1810 | // If the operation is with the result of a select instruction, check whether |
| 1811 | // operating on either branch of the select always yields the same value. |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1812 | if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS)) |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 1813 | if (Value *V = ThreadBinOpOverSelect(Opcode, LHS, RHS, TD, DT, |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1814 | MaxRecurse)) |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1815 | return V; |
| 1816 | |
| 1817 | // If the operation is with the result of a phi instruction, check whether |
| 1818 | // operating on all incoming values of the phi always yields the same value. |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1819 | if (isa<PHINode>(LHS) || isa<PHINode>(RHS)) |
| 1820 | if (Value *V = ThreadBinOpOverPHI(Opcode, LHS, RHS, TD, DT, MaxRecurse)) |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 1821 | return V; |
| 1822 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1823 | return 0; |
| 1824 | } |
| 1825 | } |
Chris Lattner | 9dbb429 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 1826 | |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1827 | Value *llvm::SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 1828 | const TargetData *TD, const DominatorTree *DT) { |
| 1829 | return ::SimplifyBinOp(Opcode, LHS, RHS, TD, DT, RecursionLimit); |
Chris Lattner | 9dbb429 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 1830 | } |
| 1831 | |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1832 | /// SimplifyCmpInst - Given operands for a CmpInst, see if we can |
| 1833 | /// fold the result. |
| 1834 | static Value *SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 1835 | const TargetData *TD, const DominatorTree *DT, |
| 1836 | unsigned MaxRecurse) { |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1837 | if (CmpInst::isIntPredicate((CmpInst::Predicate)Predicate)) |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 1838 | return SimplifyICmpInst(Predicate, LHS, RHS, TD, DT, MaxRecurse); |
| 1839 | return SimplifyFCmpInst(Predicate, LHS, RHS, TD, DT, MaxRecurse); |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1840 | } |
| 1841 | |
| 1842 | Value *llvm::SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 1843 | const TargetData *TD, const DominatorTree *DT) { |
| 1844 | return ::SimplifyCmpInst(Predicate, LHS, RHS, TD, DT, RecursionLimit); |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1845 | } |
Chris Lattner | e345378 | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 1846 | |
| 1847 | /// SimplifyInstruction - See if we can compute a simplified version of this |
| 1848 | /// instruction. If not, this returns null. |
Duncan Sands | eff0581 | 2010-11-14 18:36:10 +0000 | [diff] [blame] | 1849 | Value *llvm::SimplifyInstruction(Instruction *I, const TargetData *TD, |
| 1850 | const DominatorTree *DT) { |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 1851 | Value *Result; |
| 1852 | |
Chris Lattner | e345378 | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 1853 | switch (I->getOpcode()) { |
| 1854 | default: |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 1855 | Result = ConstantFoldInstruction(I, TD); |
| 1856 | break; |
Chris Lattner | 8aee8ef | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 1857 | case Instruction::Add: |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 1858 | Result = SimplifyAddInst(I->getOperand(0), I->getOperand(1), |
| 1859 | cast<BinaryOperator>(I)->hasNoSignedWrap(), |
| 1860 | cast<BinaryOperator>(I)->hasNoUnsignedWrap(), |
| 1861 | TD, DT); |
| 1862 | break; |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 1863 | case Instruction::Sub: |
| 1864 | Result = SimplifySubInst(I->getOperand(0), I->getOperand(1), |
| 1865 | cast<BinaryOperator>(I)->hasNoSignedWrap(), |
| 1866 | cast<BinaryOperator>(I)->hasNoUnsignedWrap(), |
| 1867 | TD, DT); |
| 1868 | break; |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 1869 | case Instruction::Mul: |
| 1870 | Result = SimplifyMulInst(I->getOperand(0), I->getOperand(1), TD, DT); |
| 1871 | break; |
Duncan Sands | 593faa5 | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1872 | case Instruction::SDiv: |
| 1873 | Result = SimplifySDivInst(I->getOperand(0), I->getOperand(1), TD, DT); |
| 1874 | break; |
| 1875 | case Instruction::UDiv: |
| 1876 | Result = SimplifyUDivInst(I->getOperand(0), I->getOperand(1), TD, DT); |
| 1877 | break; |
Frits van Bommel | 1fca2c3 | 2011-01-29 15:26:31 +0000 | [diff] [blame] | 1878 | case Instruction::FDiv: |
| 1879 | Result = SimplifyFDivInst(I->getOperand(0), I->getOperand(1), TD, DT); |
| 1880 | break; |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1881 | case Instruction::Shl: |
| 1882 | Result = SimplifyShlInst(I->getOperand(0), I->getOperand(1), TD, DT); |
| 1883 | break; |
| 1884 | case Instruction::LShr: |
| 1885 | Result = SimplifyLShrInst(I->getOperand(0), I->getOperand(1), TD, DT); |
| 1886 | break; |
| 1887 | case Instruction::AShr: |
| 1888 | Result = SimplifyAShrInst(I->getOperand(0), I->getOperand(1), TD, DT); |
| 1889 | break; |
Chris Lattner | e345378 | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 1890 | case Instruction::And: |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 1891 | Result = SimplifyAndInst(I->getOperand(0), I->getOperand(1), TD, DT); |
| 1892 | break; |
Chris Lattner | e345378 | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 1893 | case Instruction::Or: |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 1894 | Result = SimplifyOrInst(I->getOperand(0), I->getOperand(1), TD, DT); |
| 1895 | break; |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1896 | case Instruction::Xor: |
| 1897 | Result = SimplifyXorInst(I->getOperand(0), I->getOperand(1), TD, DT); |
| 1898 | break; |
Chris Lattner | e345378 | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 1899 | case Instruction::ICmp: |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 1900 | Result = SimplifyICmpInst(cast<ICmpInst>(I)->getPredicate(), |
| 1901 | I->getOperand(0), I->getOperand(1), TD, DT); |
| 1902 | break; |
Chris Lattner | e345378 | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 1903 | case Instruction::FCmp: |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 1904 | Result = SimplifyFCmpInst(cast<FCmpInst>(I)->getPredicate(), |
| 1905 | I->getOperand(0), I->getOperand(1), TD, DT); |
| 1906 | break; |
Chris Lattner | 0475426 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 1907 | case Instruction::Select: |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 1908 | Result = SimplifySelectInst(I->getOperand(0), I->getOperand(1), |
| 1909 | I->getOperand(2), TD, DT); |
| 1910 | break; |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 1911 | case Instruction::GetElementPtr: { |
| 1912 | SmallVector<Value*, 8> Ops(I->op_begin(), I->op_end()); |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 1913 | Result = SimplifyGEPInst(&Ops[0], Ops.size(), TD, DT); |
| 1914 | break; |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 1915 | } |
Duncan Sands | cd6636c | 2010-11-14 13:30:18 +0000 | [diff] [blame] | 1916 | case Instruction::PHI: |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 1917 | Result = SimplifyPHINode(cast<PHINode>(I), DT); |
| 1918 | break; |
Chris Lattner | e345378 | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 1919 | } |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 1920 | |
| 1921 | /// If called on unreachable code, the above logic may report that the |
| 1922 | /// instruction simplified to itself. Make life easier for users by |
Duncan Sands | f8b1a5e | 2010-12-15 11:02:22 +0000 | [diff] [blame] | 1923 | /// detecting that case here, returning a safe value instead. |
| 1924 | return Result == I ? UndefValue::get(I->getType()) : Result; |
Chris Lattner | e345378 | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 1925 | } |
| 1926 | |
Chris Lattner | 40d8c28 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 1927 | /// ReplaceAndSimplifyAllUses - Perform From->replaceAllUsesWith(To) and then |
| 1928 | /// delete the From instruction. In addition to a basic RAUW, this does a |
| 1929 | /// recursive simplification of the newly formed instructions. This catches |
| 1930 | /// things where one simplification exposes other opportunities. This only |
| 1931 | /// simplifies and deletes scalar operations, it does not change the CFG. |
| 1932 | /// |
| 1933 | void llvm::ReplaceAndSimplifyAllUses(Instruction *From, Value *To, |
Duncan Sands | eff0581 | 2010-11-14 18:36:10 +0000 | [diff] [blame] | 1934 | const TargetData *TD, |
| 1935 | const DominatorTree *DT) { |
Chris Lattner | 40d8c28 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 1936 | assert(From != To && "ReplaceAndSimplifyAllUses(X,X) is not valid!"); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1937 | |
Chris Lattner | d2bfe54 | 2010-07-15 06:36:08 +0000 | [diff] [blame] | 1938 | // FromHandle/ToHandle - This keeps a WeakVH on the from/to values so that |
| 1939 | // we can know if it gets deleted out from under us or replaced in a |
| 1940 | // recursive simplification. |
Chris Lattner | 40d8c28 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 1941 | WeakVH FromHandle(From); |
Chris Lattner | d2bfe54 | 2010-07-15 06:36:08 +0000 | [diff] [blame] | 1942 | WeakVH ToHandle(To); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1943 | |
Chris Lattner | 40d8c28 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 1944 | while (!From->use_empty()) { |
| 1945 | // Update the instruction to use the new value. |
Chris Lattner | d2bfe54 | 2010-07-15 06:36:08 +0000 | [diff] [blame] | 1946 | Use &TheUse = From->use_begin().getUse(); |
| 1947 | Instruction *User = cast<Instruction>(TheUse.getUser()); |
| 1948 | TheUse = To; |
| 1949 | |
| 1950 | // Check to see if the instruction can be folded due to the operand |
| 1951 | // replacement. For example changing (or X, Y) into (or X, -1) can replace |
| 1952 | // the 'or' with -1. |
| 1953 | Value *SimplifiedVal; |
| 1954 | { |
| 1955 | // Sanity check to make sure 'User' doesn't dangle across |
| 1956 | // SimplifyInstruction. |
| 1957 | AssertingVH<> UserHandle(User); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1958 | |
Duncan Sands | eff0581 | 2010-11-14 18:36:10 +0000 | [diff] [blame] | 1959 | SimplifiedVal = SimplifyInstruction(User, TD, DT); |
Chris Lattner | d2bfe54 | 2010-07-15 06:36:08 +0000 | [diff] [blame] | 1960 | if (SimplifiedVal == 0) continue; |
Chris Lattner | 40d8c28 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 1961 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1962 | |
Chris Lattner | d2bfe54 | 2010-07-15 06:36:08 +0000 | [diff] [blame] | 1963 | // Recursively simplify this user to the new value. |
Duncan Sands | eff0581 | 2010-11-14 18:36:10 +0000 | [diff] [blame] | 1964 | ReplaceAndSimplifyAllUses(User, SimplifiedVal, TD, DT); |
Chris Lattner | d2bfe54 | 2010-07-15 06:36:08 +0000 | [diff] [blame] | 1965 | From = dyn_cast_or_null<Instruction>((Value*)FromHandle); |
| 1966 | To = ToHandle; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1967 | |
Chris Lattner | d2bfe54 | 2010-07-15 06:36:08 +0000 | [diff] [blame] | 1968 | assert(ToHandle && "To value deleted by recursive simplification?"); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1969 | |
Chris Lattner | d2bfe54 | 2010-07-15 06:36:08 +0000 | [diff] [blame] | 1970 | // If the recursive simplification ended up revisiting and deleting |
| 1971 | // 'From' then we're done. |
| 1972 | if (From == 0) |
| 1973 | return; |
Chris Lattner | 40d8c28 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 1974 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1975 | |
Chris Lattner | d2bfe54 | 2010-07-15 06:36:08 +0000 | [diff] [blame] | 1976 | // If 'From' has value handles referring to it, do a real RAUW to update them. |
| 1977 | From->replaceAllUsesWith(To); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1978 | |
Chris Lattner | 40d8c28 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 1979 | From->eraseFromParent(); |
| 1980 | } |