Arnold Schwaighofer | e972d03 | 2013-05-05 01:54:46 +0000 | [diff] [blame] | 1 | //===---- llvm/unittest/IR/PatternMatch.cpp - PatternMatch unit tests ----===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Arnold Schwaighofer | e972d03 | 2013-05-05 01:54:46 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Chandler Carruth | 9a67b07 | 2017-06-06 11:06:56 +0000 | [diff] [blame] | 9 | #include "llvm/IR/PatternMatch.h" |
Arnold Schwaighofer | e972d03 | 2013-05-05 01:54:46 +0000 | [diff] [blame] | 10 | #include "llvm/ADT/STLExtras.h" |
| 11 | #include "llvm/Analysis/ValueTracking.h" |
| 12 | #include "llvm/IR/BasicBlock.h" |
| 13 | #include "llvm/IR/Constants.h" |
| 14 | #include "llvm/IR/DataLayout.h" |
| 15 | #include "llvm/IR/DerivedTypes.h" |
Chandler Carruth | 4603e96a | 2014-01-05 02:07:20 +0000 | [diff] [blame] | 16 | #include "llvm/IR/Function.h" |
Arnold Schwaighofer | e972d03 | 2013-05-05 01:54:46 +0000 | [diff] [blame] | 17 | #include "llvm/IR/IRBuilder.h" |
Chandler Carruth | 4603e96a | 2014-01-05 02:07:20 +0000 | [diff] [blame] | 18 | #include "llvm/IR/Instructions.h" |
Arnold Schwaighofer | e972d03 | 2013-05-05 01:54:46 +0000 | [diff] [blame] | 19 | #include "llvm/IR/LLVMContext.h" |
| 20 | #include "llvm/IR/MDBuilder.h" |
Chandler Carruth | 4603e96a | 2014-01-05 02:07:20 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Module.h" |
Chandler Carruth | 64396b0 | 2014-03-04 12:05:47 +0000 | [diff] [blame] | 22 | #include "llvm/IR/NoFolder.h" |
Arnold Schwaighofer | e972d03 | 2013-05-05 01:54:46 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Operator.h" |
Chandler Carruth | 4603e96a | 2014-01-05 02:07:20 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Type.h" |
Arnold Schwaighofer | e972d03 | 2013-05-05 01:54:46 +0000 | [diff] [blame] | 25 | #include "gtest/gtest.h" |
| 26 | |
Chandler Carruth | 4603e96a | 2014-01-05 02:07:20 +0000 | [diff] [blame] | 27 | using namespace llvm; |
Arnold Schwaighofer | e972d03 | 2013-05-05 01:54:46 +0000 | [diff] [blame] | 28 | using namespace llvm::PatternMatch; |
| 29 | |
Arnold Schwaighofer | e972d03 | 2013-05-05 01:54:46 +0000 | [diff] [blame] | 30 | namespace { |
| 31 | |
Chandler Carruth | 4603e96a | 2014-01-05 02:07:20 +0000 | [diff] [blame] | 32 | struct PatternMatchTest : ::testing::Test { |
| 33 | LLVMContext Ctx; |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 34 | std::unique_ptr<Module> M; |
Chandler Carruth | 4603e96a | 2014-01-05 02:07:20 +0000 | [diff] [blame] | 35 | Function *F; |
| 36 | BasicBlock *BB; |
Mehdi Amini | ba9fba8 | 2016-03-13 21:05:13 +0000 | [diff] [blame] | 37 | IRBuilder<NoFolder> IRB; |
Arnold Schwaighofer | e972d03 | 2013-05-05 01:54:46 +0000 | [diff] [blame] | 38 | |
Chandler Carruth | 4603e96a | 2014-01-05 02:07:20 +0000 | [diff] [blame] | 39 | PatternMatchTest() |
| 40 | : M(new Module("PatternMatchTestModule", Ctx)), |
| 41 | F(Function::Create( |
| 42 | FunctionType::get(Type::getVoidTy(Ctx), /* IsVarArg */ false), |
| 43 | Function::ExternalLinkage, "f", M.get())), |
Ilya Biryukov | 7284d44 | 2019-07-15 16:43:36 +0000 | [diff] [blame] | 44 | BB(BasicBlock::Create(Ctx, "entry", F)), IRB(BB) {} |
Chandler Carruth | 4603e96a | 2014-01-05 02:07:20 +0000 | [diff] [blame] | 45 | }; |
Arnold Schwaighofer | e972d03 | 2013-05-05 01:54:46 +0000 | [diff] [blame] | 46 | |
Chandler Carruth | cde91b4 | 2014-01-05 09:14:53 +0000 | [diff] [blame] | 47 | TEST_F(PatternMatchTest, OneUse) { |
| 48 | // Build up a little tree of values: |
| 49 | // |
| 50 | // One = (1 + 2) + 42 |
| 51 | // Two = One + 42 |
| 52 | // Leaf = (Two + 8) + (Two + 13) |
| 53 | Value *One = IRB.CreateAdd(IRB.CreateAdd(IRB.getInt32(1), IRB.getInt32(2)), |
| 54 | IRB.getInt32(42)); |
| 55 | Value *Two = IRB.CreateAdd(One, IRB.getInt32(42)); |
| 56 | Value *Leaf = IRB.CreateAdd(IRB.CreateAdd(Two, IRB.getInt32(8)), |
| 57 | IRB.CreateAdd(Two, IRB.getInt32(13))); |
| 58 | Value *V; |
| 59 | |
| 60 | EXPECT_TRUE(m_OneUse(m_Value(V)).match(One)); |
| 61 | EXPECT_EQ(One, V); |
| 62 | |
| 63 | EXPECT_FALSE(m_OneUse(m_Value()).match(Two)); |
| 64 | EXPECT_FALSE(m_OneUse(m_Value()).match(Leaf)); |
| 65 | } |
| 66 | |
Roman Lebedev | c5f92bd | 2019-07-10 16:07:35 +0000 | [diff] [blame] | 67 | TEST_F(PatternMatchTest, SpecificIntEQ) { |
| 68 | Type *IntTy = IRB.getInt32Ty(); |
| 69 | unsigned BitWidth = IntTy->getScalarSizeInBits(); |
| 70 | |
| 71 | Value *Zero = ConstantInt::get(IntTy, 0); |
| 72 | Value *One = ConstantInt::get(IntTy, 1); |
| 73 | Value *NegOne = ConstantInt::get(IntTy, -1); |
| 74 | |
| 75 | EXPECT_TRUE( |
| 76 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, 0)) |
| 77 | .match(Zero)); |
| 78 | EXPECT_FALSE( |
| 79 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, 0)) |
| 80 | .match(One)); |
| 81 | EXPECT_FALSE( |
| 82 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, 0)) |
| 83 | .match(NegOne)); |
| 84 | |
| 85 | EXPECT_FALSE( |
| 86 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, 1)) |
| 87 | .match(Zero)); |
| 88 | EXPECT_TRUE( |
| 89 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, 1)) |
| 90 | .match(One)); |
| 91 | EXPECT_FALSE( |
| 92 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, 1)) |
| 93 | .match(NegOne)); |
| 94 | |
| 95 | EXPECT_FALSE( |
| 96 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, -1)) |
| 97 | .match(Zero)); |
| 98 | EXPECT_FALSE( |
| 99 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, -1)) |
| 100 | .match(One)); |
| 101 | EXPECT_TRUE( |
| 102 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, -1)) |
| 103 | .match(NegOne)); |
| 104 | } |
| 105 | |
| 106 | TEST_F(PatternMatchTest, SpecificIntNE) { |
| 107 | Type *IntTy = IRB.getInt32Ty(); |
| 108 | unsigned BitWidth = IntTy->getScalarSizeInBits(); |
| 109 | |
| 110 | Value *Zero = ConstantInt::get(IntTy, 0); |
| 111 | Value *One = ConstantInt::get(IntTy, 1); |
| 112 | Value *NegOne = ConstantInt::get(IntTy, -1); |
| 113 | |
| 114 | EXPECT_FALSE( |
| 115 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, 0)) |
| 116 | .match(Zero)); |
| 117 | EXPECT_TRUE( |
| 118 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, 0)) |
| 119 | .match(One)); |
| 120 | EXPECT_TRUE( |
| 121 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, 0)) |
| 122 | .match(NegOne)); |
| 123 | |
| 124 | EXPECT_TRUE( |
| 125 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, 1)) |
| 126 | .match(Zero)); |
| 127 | EXPECT_FALSE( |
| 128 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, 1)) |
| 129 | .match(One)); |
| 130 | EXPECT_TRUE( |
| 131 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, 1)) |
| 132 | .match(NegOne)); |
| 133 | |
| 134 | EXPECT_TRUE( |
| 135 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, -1)) |
| 136 | .match(Zero)); |
| 137 | EXPECT_TRUE( |
| 138 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, -1)) |
| 139 | .match(One)); |
| 140 | EXPECT_FALSE( |
| 141 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, -1)) |
| 142 | .match(NegOne)); |
| 143 | } |
| 144 | |
| 145 | TEST_F(PatternMatchTest, SpecificIntUGT) { |
| 146 | Type *IntTy = IRB.getInt32Ty(); |
| 147 | unsigned BitWidth = IntTy->getScalarSizeInBits(); |
| 148 | |
| 149 | Value *Zero = ConstantInt::get(IntTy, 0); |
| 150 | Value *One = ConstantInt::get(IntTy, 1); |
| 151 | Value *NegOne = ConstantInt::get(IntTy, -1); |
| 152 | |
| 153 | EXPECT_FALSE( |
| 154 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, 0)) |
| 155 | .match(Zero)); |
| 156 | EXPECT_TRUE( |
| 157 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, 0)) |
| 158 | .match(One)); |
| 159 | EXPECT_TRUE( |
| 160 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, 0)) |
| 161 | .match(NegOne)); |
| 162 | |
| 163 | EXPECT_FALSE( |
| 164 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, 1)) |
| 165 | .match(Zero)); |
| 166 | EXPECT_FALSE( |
| 167 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, 1)) |
| 168 | .match(One)); |
| 169 | EXPECT_TRUE( |
| 170 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, 1)) |
| 171 | .match(NegOne)); |
| 172 | |
| 173 | EXPECT_FALSE( |
| 174 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, -1)) |
| 175 | .match(Zero)); |
| 176 | EXPECT_FALSE( |
| 177 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, -1)) |
| 178 | .match(One)); |
| 179 | EXPECT_FALSE( |
| 180 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, -1)) |
| 181 | .match(NegOne)); |
| 182 | } |
| 183 | |
| 184 | TEST_F(PatternMatchTest, SpecificIntUGE) { |
| 185 | Type *IntTy = IRB.getInt32Ty(); |
| 186 | unsigned BitWidth = IntTy->getScalarSizeInBits(); |
| 187 | |
| 188 | Value *Zero = ConstantInt::get(IntTy, 0); |
| 189 | Value *One = ConstantInt::get(IntTy, 1); |
| 190 | Value *NegOne = ConstantInt::get(IntTy, -1); |
| 191 | |
| 192 | EXPECT_TRUE( |
| 193 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, 0)) |
| 194 | .match(Zero)); |
| 195 | EXPECT_TRUE( |
| 196 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, 0)) |
| 197 | .match(One)); |
| 198 | EXPECT_TRUE( |
| 199 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, 0)) |
| 200 | .match(NegOne)); |
| 201 | |
| 202 | EXPECT_FALSE( |
| 203 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, 1)) |
| 204 | .match(Zero)); |
| 205 | EXPECT_TRUE( |
| 206 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, 1)) |
| 207 | .match(One)); |
| 208 | EXPECT_TRUE( |
| 209 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, 1)) |
| 210 | .match(NegOne)); |
| 211 | |
| 212 | EXPECT_FALSE( |
| 213 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, -1)) |
| 214 | .match(Zero)); |
| 215 | EXPECT_FALSE( |
| 216 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, -1)) |
| 217 | .match(One)); |
| 218 | EXPECT_TRUE( |
| 219 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, -1)) |
| 220 | .match(NegOne)); |
| 221 | } |
| 222 | |
Roman Lebedev | fe107fc | 2019-06-29 11:51:37 +0000 | [diff] [blame] | 223 | TEST_F(PatternMatchTest, SpecificIntULT) { |
| 224 | Type *IntTy = IRB.getInt32Ty(); |
| 225 | unsigned BitWidth = IntTy->getScalarSizeInBits(); |
| 226 | |
| 227 | Value *Zero = ConstantInt::get(IntTy, 0); |
| 228 | Value *One = ConstantInt::get(IntTy, 1); |
| 229 | Value *NegOne = ConstantInt::get(IntTy, -1); |
| 230 | |
Roman Lebedev | c5f92bd | 2019-07-10 16:07:35 +0000 | [diff] [blame] | 231 | EXPECT_FALSE( |
| 232 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, 0)) |
| 233 | .match(Zero)); |
| 234 | EXPECT_FALSE( |
| 235 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, 0)) |
| 236 | .match(One)); |
| 237 | EXPECT_FALSE( |
| 238 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, 0)) |
| 239 | .match(NegOne)); |
Roman Lebedev | fe107fc | 2019-06-29 11:51:37 +0000 | [diff] [blame] | 240 | |
Roman Lebedev | c5f92bd | 2019-07-10 16:07:35 +0000 | [diff] [blame] | 241 | EXPECT_TRUE( |
| 242 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, 1)) |
| 243 | .match(Zero)); |
| 244 | EXPECT_FALSE( |
| 245 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, 1)) |
| 246 | .match(One)); |
| 247 | EXPECT_FALSE( |
| 248 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, 1)) |
| 249 | .match(NegOne)); |
Roman Lebedev | fe107fc | 2019-06-29 11:51:37 +0000 | [diff] [blame] | 250 | |
Roman Lebedev | c5f92bd | 2019-07-10 16:07:35 +0000 | [diff] [blame] | 251 | EXPECT_TRUE( |
| 252 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, -1)) |
| 253 | .match(Zero)); |
| 254 | EXPECT_TRUE( |
| 255 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, -1)) |
| 256 | .match(One)); |
| 257 | EXPECT_FALSE( |
| 258 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, -1)) |
| 259 | .match(NegOne)); |
| 260 | } |
| 261 | |
| 262 | TEST_F(PatternMatchTest, SpecificIntULE) { |
| 263 | Type *IntTy = IRB.getInt32Ty(); |
| 264 | unsigned BitWidth = IntTy->getScalarSizeInBits(); |
| 265 | |
| 266 | Value *Zero = ConstantInt::get(IntTy, 0); |
| 267 | Value *One = ConstantInt::get(IntTy, 1); |
| 268 | Value *NegOne = ConstantInt::get(IntTy, -1); |
| 269 | |
| 270 | EXPECT_TRUE( |
| 271 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, 0)) |
| 272 | .match(Zero)); |
| 273 | EXPECT_FALSE( |
| 274 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, 0)) |
| 275 | .match(One)); |
| 276 | EXPECT_FALSE( |
| 277 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, 0)) |
| 278 | .match(NegOne)); |
| 279 | |
| 280 | EXPECT_TRUE( |
| 281 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, 1)) |
| 282 | .match(Zero)); |
| 283 | EXPECT_TRUE( |
| 284 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, 1)) |
| 285 | .match(One)); |
| 286 | EXPECT_FALSE( |
| 287 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, 1)) |
| 288 | .match(NegOne)); |
| 289 | |
| 290 | EXPECT_TRUE( |
| 291 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, -1)) |
| 292 | .match(Zero)); |
| 293 | EXPECT_TRUE( |
| 294 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, -1)) |
| 295 | .match(One)); |
| 296 | EXPECT_TRUE( |
| 297 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, -1)) |
| 298 | .match(NegOne)); |
| 299 | } |
| 300 | |
| 301 | TEST_F(PatternMatchTest, SpecificIntSGT) { |
| 302 | Type *IntTy = IRB.getInt32Ty(); |
| 303 | unsigned BitWidth = IntTy->getScalarSizeInBits(); |
| 304 | |
| 305 | Value *Zero = ConstantInt::get(IntTy, 0); |
| 306 | Value *One = ConstantInt::get(IntTy, 1); |
| 307 | Value *NegOne = ConstantInt::get(IntTy, -1); |
| 308 | |
| 309 | EXPECT_FALSE( |
| 310 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, 0)) |
| 311 | .match(Zero)); |
| 312 | EXPECT_TRUE( |
| 313 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, 0)) |
| 314 | .match(One)); |
| 315 | EXPECT_FALSE( |
| 316 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, 0)) |
| 317 | .match(NegOne)); |
| 318 | |
| 319 | EXPECT_FALSE( |
| 320 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, 1)) |
| 321 | .match(Zero)); |
| 322 | EXPECT_FALSE( |
| 323 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, 1)) |
| 324 | .match(One)); |
| 325 | EXPECT_FALSE( |
| 326 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, 1)) |
| 327 | .match(NegOne)); |
| 328 | |
| 329 | EXPECT_TRUE( |
| 330 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, -1)) |
| 331 | .match(Zero)); |
| 332 | EXPECT_TRUE( |
| 333 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, -1)) |
| 334 | .match(One)); |
| 335 | EXPECT_FALSE( |
| 336 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, -1)) |
| 337 | .match(NegOne)); |
| 338 | } |
| 339 | |
| 340 | TEST_F(PatternMatchTest, SpecificIntSGE) { |
| 341 | Type *IntTy = IRB.getInt32Ty(); |
| 342 | unsigned BitWidth = IntTy->getScalarSizeInBits(); |
| 343 | |
| 344 | Value *Zero = ConstantInt::get(IntTy, 0); |
| 345 | Value *One = ConstantInt::get(IntTy, 1); |
| 346 | Value *NegOne = ConstantInt::get(IntTy, -1); |
| 347 | |
| 348 | EXPECT_TRUE( |
| 349 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, 0)) |
| 350 | .match(Zero)); |
| 351 | EXPECT_TRUE( |
| 352 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, 0)) |
| 353 | .match(One)); |
| 354 | EXPECT_FALSE( |
| 355 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, 0)) |
| 356 | .match(NegOne)); |
| 357 | |
| 358 | EXPECT_FALSE( |
| 359 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, 1)) |
| 360 | .match(Zero)); |
| 361 | EXPECT_TRUE( |
| 362 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, 1)) |
| 363 | .match(One)); |
| 364 | EXPECT_FALSE( |
| 365 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, 1)) |
| 366 | .match(NegOne)); |
| 367 | |
| 368 | EXPECT_TRUE( |
| 369 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, -1)) |
| 370 | .match(Zero)); |
| 371 | EXPECT_TRUE( |
| 372 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, -1)) |
| 373 | .match(One)); |
| 374 | EXPECT_TRUE( |
| 375 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, -1)) |
| 376 | .match(NegOne)); |
| 377 | } |
| 378 | |
| 379 | TEST_F(PatternMatchTest, SpecificIntSLT) { |
| 380 | Type *IntTy = IRB.getInt32Ty(); |
| 381 | unsigned BitWidth = IntTy->getScalarSizeInBits(); |
| 382 | |
| 383 | Value *Zero = ConstantInt::get(IntTy, 0); |
| 384 | Value *One = ConstantInt::get(IntTy, 1); |
| 385 | Value *NegOne = ConstantInt::get(IntTy, -1); |
| 386 | |
| 387 | EXPECT_FALSE( |
| 388 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, 0)) |
| 389 | .match(Zero)); |
| 390 | EXPECT_FALSE( |
| 391 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, 0)) |
| 392 | .match(One)); |
| 393 | EXPECT_TRUE( |
| 394 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, 0)) |
| 395 | .match(NegOne)); |
| 396 | |
| 397 | EXPECT_TRUE( |
| 398 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, 1)) |
| 399 | .match(Zero)); |
| 400 | EXPECT_FALSE( |
| 401 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, 1)) |
| 402 | .match(One)); |
| 403 | EXPECT_TRUE( |
| 404 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, 1)) |
| 405 | .match(NegOne)); |
| 406 | |
| 407 | EXPECT_FALSE( |
| 408 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, -1)) |
| 409 | .match(Zero)); |
| 410 | EXPECT_FALSE( |
| 411 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, -1)) |
| 412 | .match(One)); |
| 413 | EXPECT_FALSE( |
| 414 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, -1)) |
| 415 | .match(NegOne)); |
| 416 | } |
| 417 | |
| 418 | TEST_F(PatternMatchTest, SpecificIntSLE) { |
| 419 | Type *IntTy = IRB.getInt32Ty(); |
| 420 | unsigned BitWidth = IntTy->getScalarSizeInBits(); |
| 421 | |
| 422 | Value *Zero = ConstantInt::get(IntTy, 0); |
| 423 | Value *One = ConstantInt::get(IntTy, 1); |
| 424 | Value *NegOne = ConstantInt::get(IntTy, -1); |
| 425 | |
| 426 | EXPECT_TRUE( |
| 427 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, 0)) |
| 428 | .match(Zero)); |
| 429 | EXPECT_FALSE( |
| 430 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, 0)) |
| 431 | .match(One)); |
| 432 | EXPECT_TRUE( |
| 433 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, 0)) |
| 434 | .match(NegOne)); |
| 435 | |
| 436 | EXPECT_TRUE( |
| 437 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, 1)) |
| 438 | .match(Zero)); |
| 439 | EXPECT_TRUE( |
| 440 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, 1)) |
| 441 | .match(One)); |
| 442 | EXPECT_TRUE( |
| 443 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, 1)) |
| 444 | .match(NegOne)); |
| 445 | |
| 446 | EXPECT_FALSE( |
| 447 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, -1)) |
| 448 | .match(Zero)); |
| 449 | EXPECT_FALSE( |
| 450 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, -1)) |
| 451 | .match(One)); |
| 452 | EXPECT_TRUE( |
| 453 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, -1)) |
| 454 | .match(NegOne)); |
Roman Lebedev | fe107fc | 2019-06-29 11:51:37 +0000 | [diff] [blame] | 455 | } |
| 456 | |
Roman Lebedev | 6df3fc5 | 2019-07-25 13:34:14 +0000 | [diff] [blame^] | 457 | TEST_F(PatternMatchTest, Unless) { |
| 458 | Value *X = IRB.CreateAdd(IRB.getInt32(1), IRB.getInt32(0)); |
| 459 | |
| 460 | EXPECT_TRUE(m_Add(m_One(), m_Zero()).match(X)); |
| 461 | EXPECT_FALSE(m_Add(m_Zero(), m_One()).match(X)); |
| 462 | |
| 463 | EXPECT_FALSE(m_Unless(m_Add(m_One(), m_Zero())).match(X)); |
| 464 | EXPECT_TRUE(m_Unless(m_Add(m_Zero(), m_One())).match(X)); |
| 465 | |
| 466 | EXPECT_TRUE(m_c_Add(m_One(), m_Zero()).match(X)); |
| 467 | EXPECT_TRUE(m_c_Add(m_Zero(), m_One()).match(X)); |
| 468 | |
| 469 | EXPECT_FALSE(m_Unless(m_c_Add(m_One(), m_Zero())).match(X)); |
| 470 | EXPECT_FALSE(m_Unless(m_c_Add(m_Zero(), m_One())).match(X)); |
| 471 | } |
| 472 | |
Roman Lebedev | 6959b8e | 2018-04-27 21:23:20 +0000 | [diff] [blame] | 473 | TEST_F(PatternMatchTest, CommutativeDeferredValue) { |
| 474 | Value *X = IRB.getInt32(1); |
| 475 | Value *Y = IRB.getInt32(2); |
| 476 | |
| 477 | { |
| 478 | Value *tX = X; |
| 479 | EXPECT_TRUE(match(X, m_Deferred(tX))); |
| 480 | EXPECT_FALSE(match(Y, m_Deferred(tX))); |
| 481 | } |
| 482 | { |
| 483 | const Value *tX = X; |
| 484 | EXPECT_TRUE(match(X, m_Deferred(tX))); |
| 485 | EXPECT_FALSE(match(Y, m_Deferred(tX))); |
| 486 | } |
| 487 | { |
| 488 | Value *const tX = X; |
| 489 | EXPECT_TRUE(match(X, m_Deferred(tX))); |
| 490 | EXPECT_FALSE(match(Y, m_Deferred(tX))); |
| 491 | } |
| 492 | { |
| 493 | const Value *const tX = X; |
| 494 | EXPECT_TRUE(match(X, m_Deferred(tX))); |
| 495 | EXPECT_FALSE(match(Y, m_Deferred(tX))); |
| 496 | } |
| 497 | |
| 498 | { |
| 499 | Value *tX = nullptr; |
| 500 | EXPECT_TRUE(match(IRB.CreateAnd(X, X), m_And(m_Value(tX), m_Deferred(tX)))); |
| 501 | EXPECT_EQ(tX, X); |
| 502 | } |
| 503 | { |
| 504 | Value *tX = nullptr; |
| 505 | EXPECT_FALSE( |
| 506 | match(IRB.CreateAnd(X, Y), m_c_And(m_Value(tX), m_Deferred(tX)))); |
| 507 | } |
| 508 | |
| 509 | auto checkMatch = [X, Y](Value *Pattern) { |
| 510 | Value *tX = nullptr, *tY = nullptr; |
| 511 | EXPECT_TRUE(match( |
| 512 | Pattern, m_c_And(m_Value(tX), m_c_And(m_Deferred(tX), m_Value(tY))))); |
| 513 | EXPECT_EQ(tX, X); |
| 514 | EXPECT_EQ(tY, Y); |
| 515 | }; |
| 516 | |
| 517 | checkMatch(IRB.CreateAnd(X, IRB.CreateAnd(X, Y))); |
| 518 | checkMatch(IRB.CreateAnd(X, IRB.CreateAnd(Y, X))); |
| 519 | checkMatch(IRB.CreateAnd(IRB.CreateAnd(X, Y), X)); |
| 520 | checkMatch(IRB.CreateAnd(IRB.CreateAnd(Y, X), X)); |
| 521 | } |
| 522 | |
Chandler Carruth | 4603e96a | 2014-01-05 02:07:20 +0000 | [diff] [blame] | 523 | TEST_F(PatternMatchTest, FloatingPointOrderedMin) { |
Chandler Carruth | 91f4e60 | 2014-01-05 02:23:11 +0000 | [diff] [blame] | 524 | Type *FltTy = IRB.getFloatTy(); |
Arnold Schwaighofer | e972d03 | 2013-05-05 01:54:46 +0000 | [diff] [blame] | 525 | Value *L = ConstantFP::get(FltTy, 1.0); |
| 526 | Value *R = ConstantFP::get(FltTy, 2.0); |
Chandler Carruth | 4603e96a | 2014-01-05 02:07:20 +0000 | [diff] [blame] | 527 | Value *MatchL, *MatchR; |
Arnold Schwaighofer | e972d03 | 2013-05-05 01:54:46 +0000 | [diff] [blame] | 528 | |
| 529 | // Test OLT. |
Chandler Carruth | 91f4e60 | 2014-01-05 02:23:11 +0000 | [diff] [blame] | 530 | EXPECT_TRUE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR)) |
| 531 | .match(IRB.CreateSelect(IRB.CreateFCmpOLT(L, R), L, R))); |
Chandler Carruth | 4603e96a | 2014-01-05 02:07:20 +0000 | [diff] [blame] | 532 | EXPECT_EQ(L, MatchL); |
| 533 | EXPECT_EQ(R, MatchR); |
Arnold Schwaighofer | e972d03 | 2013-05-05 01:54:46 +0000 | [diff] [blame] | 534 | |
| 535 | // Test OLE. |
Chandler Carruth | 91f4e60 | 2014-01-05 02:23:11 +0000 | [diff] [blame] | 536 | EXPECT_TRUE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR)) |
| 537 | .match(IRB.CreateSelect(IRB.CreateFCmpOLE(L, R), L, R))); |
Chandler Carruth | 4603e96a | 2014-01-05 02:07:20 +0000 | [diff] [blame] | 538 | EXPECT_EQ(L, MatchL); |
| 539 | EXPECT_EQ(R, MatchR); |
Arnold Schwaighofer | e972d03 | 2013-05-05 01:54:46 +0000 | [diff] [blame] | 540 | |
| 541 | // Test no match on OGE. |
Chandler Carruth | 91f4e60 | 2014-01-05 02:23:11 +0000 | [diff] [blame] | 542 | EXPECT_FALSE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR)) |
| 543 | .match(IRB.CreateSelect(IRB.CreateFCmpOGE(L, R), L, R))); |
Arnold Schwaighofer | e972d03 | 2013-05-05 01:54:46 +0000 | [diff] [blame] | 544 | |
| 545 | // Test no match on OGT. |
Chandler Carruth | 91f4e60 | 2014-01-05 02:23:11 +0000 | [diff] [blame] | 546 | EXPECT_FALSE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR)) |
| 547 | .match(IRB.CreateSelect(IRB.CreateFCmpOGT(L, R), L, R))); |
Arnold Schwaighofer | e972d03 | 2013-05-05 01:54:46 +0000 | [diff] [blame] | 548 | |
Craig Topper | c663552 | 2017-06-13 17:18:45 +0000 | [diff] [blame] | 549 | // Test inverted selects. Note, that this "inverts" the ordering, e.g.: |
| 550 | // %cmp = fcmp oge L, R |
| 551 | // %min = select %cmp R, L |
| 552 | // Given L == NaN |
| 553 | // the above is expanded to %cmp == false ==> %min = L |
| 554 | // which is true for UnordFMin, not OrdFMin, so test that: |
| 555 | |
| 556 | // [OU]GE with inverted select. |
| 557 | EXPECT_FALSE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR)) |
Chandler Carruth | 91f4e60 | 2014-01-05 02:23:11 +0000 | [diff] [blame] | 558 | .match(IRB.CreateSelect(IRB.CreateFCmpOGE(L, R), R, L))); |
Craig Topper | c663552 | 2017-06-13 17:18:45 +0000 | [diff] [blame] | 559 | EXPECT_TRUE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR)) |
| 560 | .match(IRB.CreateSelect(IRB.CreateFCmpUGE(L, R), R, L))); |
Chandler Carruth | 4603e96a | 2014-01-05 02:07:20 +0000 | [diff] [blame] | 561 | EXPECT_EQ(L, MatchL); |
| 562 | EXPECT_EQ(R, MatchR); |
Arnold Schwaighofer | e972d03 | 2013-05-05 01:54:46 +0000 | [diff] [blame] | 563 | |
Craig Topper | c663552 | 2017-06-13 17:18:45 +0000 | [diff] [blame] | 564 | // [OU]GT with inverted select. |
| 565 | EXPECT_FALSE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR)) |
Chandler Carruth | 91f4e60 | 2014-01-05 02:23:11 +0000 | [diff] [blame] | 566 | .match(IRB.CreateSelect(IRB.CreateFCmpOGT(L, R), R, L))); |
Craig Topper | c663552 | 2017-06-13 17:18:45 +0000 | [diff] [blame] | 567 | EXPECT_TRUE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR)) |
| 568 | .match(IRB.CreateSelect(IRB.CreateFCmpUGT(L, R), R, L))); |
Chandler Carruth | 4603e96a | 2014-01-05 02:07:20 +0000 | [diff] [blame] | 569 | EXPECT_EQ(L, MatchL); |
| 570 | EXPECT_EQ(R, MatchR); |
Arnold Schwaighofer | e972d03 | 2013-05-05 01:54:46 +0000 | [diff] [blame] | 571 | } |
| 572 | |
Chandler Carruth | 4603e96a | 2014-01-05 02:07:20 +0000 | [diff] [blame] | 573 | TEST_F(PatternMatchTest, FloatingPointOrderedMax) { |
Chandler Carruth | 91f4e60 | 2014-01-05 02:23:11 +0000 | [diff] [blame] | 574 | Type *FltTy = IRB.getFloatTy(); |
Arnold Schwaighofer | e972d03 | 2013-05-05 01:54:46 +0000 | [diff] [blame] | 575 | Value *L = ConstantFP::get(FltTy, 1.0); |
| 576 | Value *R = ConstantFP::get(FltTy, 2.0); |
Chandler Carruth | 4603e96a | 2014-01-05 02:07:20 +0000 | [diff] [blame] | 577 | Value *MatchL, *MatchR; |
Arnold Schwaighofer | e972d03 | 2013-05-05 01:54:46 +0000 | [diff] [blame] | 578 | |
| 579 | // Test OGT. |
Chandler Carruth | 91f4e60 | 2014-01-05 02:23:11 +0000 | [diff] [blame] | 580 | EXPECT_TRUE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR)) |
| 581 | .match(IRB.CreateSelect(IRB.CreateFCmpOGT(L, R), L, R))); |
Chandler Carruth | 4603e96a | 2014-01-05 02:07:20 +0000 | [diff] [blame] | 582 | EXPECT_EQ(L, MatchL); |
| 583 | EXPECT_EQ(R, MatchR); |
Arnold Schwaighofer | e972d03 | 2013-05-05 01:54:46 +0000 | [diff] [blame] | 584 | |
| 585 | // Test OGE. |
Chandler Carruth | 91f4e60 | 2014-01-05 02:23:11 +0000 | [diff] [blame] | 586 | EXPECT_TRUE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR)) |
| 587 | .match(IRB.CreateSelect(IRB.CreateFCmpOGE(L, R), L, R))); |
Chandler Carruth | 4603e96a | 2014-01-05 02:07:20 +0000 | [diff] [blame] | 588 | EXPECT_EQ(L, MatchL); |
| 589 | EXPECT_EQ(R, MatchR); |
Arnold Schwaighofer | e972d03 | 2013-05-05 01:54:46 +0000 | [diff] [blame] | 590 | |
| 591 | // Test no match on OLE. |
Chandler Carruth | 91f4e60 | 2014-01-05 02:23:11 +0000 | [diff] [blame] | 592 | EXPECT_FALSE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR)) |
| 593 | .match(IRB.CreateSelect(IRB.CreateFCmpOLE(L, R), L, R))); |
Arnold Schwaighofer | e972d03 | 2013-05-05 01:54:46 +0000 | [diff] [blame] | 594 | |
| 595 | // Test no match on OLT. |
Chandler Carruth | 91f4e60 | 2014-01-05 02:23:11 +0000 | [diff] [blame] | 596 | EXPECT_FALSE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR)) |
| 597 | .match(IRB.CreateSelect(IRB.CreateFCmpOLT(L, R), L, R))); |
Arnold Schwaighofer | e972d03 | 2013-05-05 01:54:46 +0000 | [diff] [blame] | 598 | |
Craig Topper | c663552 | 2017-06-13 17:18:45 +0000 | [diff] [blame] | 599 | |
| 600 | // Test inverted selects. Note, that this "inverts" the ordering, e.g.: |
| 601 | // %cmp = fcmp ole L, R |
| 602 | // %max = select %cmp, R, L |
| 603 | // Given L == NaN, |
| 604 | // the above is expanded to %cmp == false ==> %max == L |
| 605 | // which is true for UnordFMax, not OrdFMax, so test that: |
| 606 | |
| 607 | // [OU]LE with inverted select. |
| 608 | EXPECT_FALSE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR)) |
| 609 | .match(IRB.CreateSelect(IRB.CreateFCmpOLE(L, R), R, L))); |
Chandler Carruth | 91f4e60 | 2014-01-05 02:23:11 +0000 | [diff] [blame] | 610 | EXPECT_TRUE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR)) |
Craig Topper | c663552 | 2017-06-13 17:18:45 +0000 | [diff] [blame] | 611 | .match(IRB.CreateSelect(IRB.CreateFCmpULE(L, R), R, L))); |
Chandler Carruth | 4603e96a | 2014-01-05 02:07:20 +0000 | [diff] [blame] | 612 | EXPECT_EQ(L, MatchL); |
| 613 | EXPECT_EQ(R, MatchR); |
Arnold Schwaighofer | e972d03 | 2013-05-05 01:54:46 +0000 | [diff] [blame] | 614 | |
Craig Topper | c663552 | 2017-06-13 17:18:45 +0000 | [diff] [blame] | 615 | // [OUT]LT with inverted select. |
| 616 | EXPECT_FALSE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR)) |
| 617 | .match(IRB.CreateSelect(IRB.CreateFCmpOLT(L, R), R, L))); |
Chandler Carruth | 91f4e60 | 2014-01-05 02:23:11 +0000 | [diff] [blame] | 618 | EXPECT_TRUE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR)) |
Craig Topper | c663552 | 2017-06-13 17:18:45 +0000 | [diff] [blame] | 619 | .match(IRB.CreateSelect(IRB.CreateFCmpULT(L, R), R, L))); |
Arnold Schwaighofer | e972d03 | 2013-05-05 01:54:46 +0000 | [diff] [blame] | 620 | EXPECT_EQ(L, MatchL); |
| 621 | EXPECT_EQ(R, MatchR); |
Arnold Schwaighofer | e972d03 | 2013-05-05 01:54:46 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Chandler Carruth | 4603e96a | 2014-01-05 02:07:20 +0000 | [diff] [blame] | 624 | TEST_F(PatternMatchTest, FloatingPointUnorderedMin) { |
Chandler Carruth | 91f4e60 | 2014-01-05 02:23:11 +0000 | [diff] [blame] | 625 | Type *FltTy = IRB.getFloatTy(); |
Arnold Schwaighofer | e972d03 | 2013-05-05 01:54:46 +0000 | [diff] [blame] | 626 | Value *L = ConstantFP::get(FltTy, 1.0); |
| 627 | Value *R = ConstantFP::get(FltTy, 2.0); |
Chandler Carruth | 4603e96a | 2014-01-05 02:07:20 +0000 | [diff] [blame] | 628 | Value *MatchL, *MatchR; |
Arnold Schwaighofer | e972d03 | 2013-05-05 01:54:46 +0000 | [diff] [blame] | 629 | |
| 630 | // Test ULT. |
Chandler Carruth | 91f4e60 | 2014-01-05 02:23:11 +0000 | [diff] [blame] | 631 | EXPECT_TRUE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR)) |
| 632 | .match(IRB.CreateSelect(IRB.CreateFCmpULT(L, R), L, R))); |
Chandler Carruth | 4603e96a | 2014-01-05 02:07:20 +0000 | [diff] [blame] | 633 | EXPECT_EQ(L, MatchL); |
| 634 | EXPECT_EQ(R, MatchR); |
Arnold Schwaighofer | e972d03 | 2013-05-05 01:54:46 +0000 | [diff] [blame] | 635 | |
| 636 | // Test ULE. |
Chandler Carruth | 91f4e60 | 2014-01-05 02:23:11 +0000 | [diff] [blame] | 637 | EXPECT_TRUE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR)) |
| 638 | .match(IRB.CreateSelect(IRB.CreateFCmpULE(L, R), L, R))); |
Chandler Carruth | 4603e96a | 2014-01-05 02:07:20 +0000 | [diff] [blame] | 639 | EXPECT_EQ(L, MatchL); |
| 640 | EXPECT_EQ(R, MatchR); |
Arnold Schwaighofer | e972d03 | 2013-05-05 01:54:46 +0000 | [diff] [blame] | 641 | |
| 642 | // Test no match on UGE. |
Chandler Carruth | 91f4e60 | 2014-01-05 02:23:11 +0000 | [diff] [blame] | 643 | EXPECT_FALSE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR)) |
| 644 | .match(IRB.CreateSelect(IRB.CreateFCmpUGE(L, R), L, R))); |
Arnold Schwaighofer | e972d03 | 2013-05-05 01:54:46 +0000 | [diff] [blame] | 645 | |
| 646 | // Test no match on UGT. |
Chandler Carruth | 91f4e60 | 2014-01-05 02:23:11 +0000 | [diff] [blame] | 647 | EXPECT_FALSE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR)) |
| 648 | .match(IRB.CreateSelect(IRB.CreateFCmpUGT(L, R), L, R))); |
Arnold Schwaighofer | e972d03 | 2013-05-05 01:54:46 +0000 | [diff] [blame] | 649 | |
Craig Topper | c663552 | 2017-06-13 17:18:45 +0000 | [diff] [blame] | 650 | // Test inverted selects. Note, that this "inverts" the ordering, e.g.: |
| 651 | // %cmp = fcmp uge L, R |
| 652 | // %min = select %cmp R, L |
| 653 | // Given L == NaN |
| 654 | // the above is expanded to %cmp == true ==> %min = R |
| 655 | // which is true for OrdFMin, not UnordFMin, so test that: |
| 656 | |
| 657 | // [UO]GE with inverted select. |
| 658 | EXPECT_FALSE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR)) |
Chandler Carruth | 91f4e60 | 2014-01-05 02:23:11 +0000 | [diff] [blame] | 659 | .match(IRB.CreateSelect(IRB.CreateFCmpUGE(L, R), R, L))); |
Craig Topper | c663552 | 2017-06-13 17:18:45 +0000 | [diff] [blame] | 660 | EXPECT_TRUE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR)) |
| 661 | .match(IRB.CreateSelect(IRB.CreateFCmpOGE(L, R), R, L))); |
Chandler Carruth | 4603e96a | 2014-01-05 02:07:20 +0000 | [diff] [blame] | 662 | EXPECT_EQ(L, MatchL); |
| 663 | EXPECT_EQ(R, MatchR); |
Arnold Schwaighofer | e972d03 | 2013-05-05 01:54:46 +0000 | [diff] [blame] | 664 | |
Craig Topper | c663552 | 2017-06-13 17:18:45 +0000 | [diff] [blame] | 665 | // [UO]GT with inverted select. |
| 666 | EXPECT_FALSE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR)) |
Chandler Carruth | 91f4e60 | 2014-01-05 02:23:11 +0000 | [diff] [blame] | 667 | .match(IRB.CreateSelect(IRB.CreateFCmpUGT(L, R), R, L))); |
Craig Topper | c663552 | 2017-06-13 17:18:45 +0000 | [diff] [blame] | 668 | EXPECT_TRUE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR)) |
| 669 | .match(IRB.CreateSelect(IRB.CreateFCmpOGT(L, R), R, L))); |
Chandler Carruth | 4603e96a | 2014-01-05 02:07:20 +0000 | [diff] [blame] | 670 | EXPECT_EQ(L, MatchL); |
| 671 | EXPECT_EQ(R, MatchR); |
Arnold Schwaighofer | e972d03 | 2013-05-05 01:54:46 +0000 | [diff] [blame] | 672 | } |
| 673 | |
Chandler Carruth | 4603e96a | 2014-01-05 02:07:20 +0000 | [diff] [blame] | 674 | TEST_F(PatternMatchTest, FloatingPointUnorderedMax) { |
Chandler Carruth | 91f4e60 | 2014-01-05 02:23:11 +0000 | [diff] [blame] | 675 | Type *FltTy = IRB.getFloatTy(); |
Arnold Schwaighofer | e972d03 | 2013-05-05 01:54:46 +0000 | [diff] [blame] | 676 | Value *L = ConstantFP::get(FltTy, 1.0); |
| 677 | Value *R = ConstantFP::get(FltTy, 2.0); |
Chandler Carruth | 4603e96a | 2014-01-05 02:07:20 +0000 | [diff] [blame] | 678 | Value *MatchL, *MatchR; |
Arnold Schwaighofer | e972d03 | 2013-05-05 01:54:46 +0000 | [diff] [blame] | 679 | |
| 680 | // Test UGT. |
Chandler Carruth | 91f4e60 | 2014-01-05 02:23:11 +0000 | [diff] [blame] | 681 | EXPECT_TRUE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR)) |
| 682 | .match(IRB.CreateSelect(IRB.CreateFCmpUGT(L, R), L, R))); |
Chandler Carruth | 4603e96a | 2014-01-05 02:07:20 +0000 | [diff] [blame] | 683 | EXPECT_EQ(L, MatchL); |
| 684 | EXPECT_EQ(R, MatchR); |
Arnold Schwaighofer | e972d03 | 2013-05-05 01:54:46 +0000 | [diff] [blame] | 685 | |
| 686 | // Test UGE. |
Chandler Carruth | 91f4e60 | 2014-01-05 02:23:11 +0000 | [diff] [blame] | 687 | EXPECT_TRUE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR)) |
| 688 | .match(IRB.CreateSelect(IRB.CreateFCmpUGE(L, R), L, R))); |
Chandler Carruth | 4603e96a | 2014-01-05 02:07:20 +0000 | [diff] [blame] | 689 | EXPECT_EQ(L, MatchL); |
| 690 | EXPECT_EQ(R, MatchR); |
Arnold Schwaighofer | e972d03 | 2013-05-05 01:54:46 +0000 | [diff] [blame] | 691 | |
| 692 | // Test no match on ULE. |
Chandler Carruth | 91f4e60 | 2014-01-05 02:23:11 +0000 | [diff] [blame] | 693 | EXPECT_FALSE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR)) |
| 694 | .match(IRB.CreateSelect(IRB.CreateFCmpULE(L, R), L, R))); |
Arnold Schwaighofer | e972d03 | 2013-05-05 01:54:46 +0000 | [diff] [blame] | 695 | |
| 696 | // Test no match on ULT. |
Chandler Carruth | 91f4e60 | 2014-01-05 02:23:11 +0000 | [diff] [blame] | 697 | EXPECT_FALSE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR)) |
| 698 | .match(IRB.CreateSelect(IRB.CreateFCmpULT(L, R), L, R))); |
Arnold Schwaighofer | e972d03 | 2013-05-05 01:54:46 +0000 | [diff] [blame] | 699 | |
Craig Topper | c663552 | 2017-06-13 17:18:45 +0000 | [diff] [blame] | 700 | // Test inverted selects. Note, that this "inverts" the ordering, e.g.: |
| 701 | // %cmp = fcmp ule L, R |
| 702 | // %max = select %cmp R, L |
| 703 | // Given L == NaN |
| 704 | // the above is expanded to %cmp == true ==> %max = R |
| 705 | // which is true for OrdFMax, not UnordFMax, so test that: |
| 706 | |
| 707 | // [UO]LE with inverted select. |
| 708 | EXPECT_FALSE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR)) |
Chandler Carruth | 91f4e60 | 2014-01-05 02:23:11 +0000 | [diff] [blame] | 709 | .match(IRB.CreateSelect(IRB.CreateFCmpULE(L, R), R, L))); |
Craig Topper | c663552 | 2017-06-13 17:18:45 +0000 | [diff] [blame] | 710 | EXPECT_TRUE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR)) |
| 711 | .match(IRB.CreateSelect(IRB.CreateFCmpOLE(L, R), R, L))); |
Chandler Carruth | 4603e96a | 2014-01-05 02:07:20 +0000 | [diff] [blame] | 712 | EXPECT_EQ(L, MatchL); |
| 713 | EXPECT_EQ(R, MatchR); |
Arnold Schwaighofer | e972d03 | 2013-05-05 01:54:46 +0000 | [diff] [blame] | 714 | |
Craig Topper | c663552 | 2017-06-13 17:18:45 +0000 | [diff] [blame] | 715 | // [UO]LT with inverted select. |
| 716 | EXPECT_FALSE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR)) |
Chandler Carruth | 91f4e60 | 2014-01-05 02:23:11 +0000 | [diff] [blame] | 717 | .match(IRB.CreateSelect(IRB.CreateFCmpULT(L, R), R, L))); |
Craig Topper | c663552 | 2017-06-13 17:18:45 +0000 | [diff] [blame] | 718 | EXPECT_TRUE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR)) |
| 719 | .match(IRB.CreateSelect(IRB.CreateFCmpOLT(L, R), R, L))); |
Chandler Carruth | 4603e96a | 2014-01-05 02:07:20 +0000 | [diff] [blame] | 720 | EXPECT_EQ(L, MatchL); |
| 721 | EXPECT_EQ(R, MatchR); |
Arnold Schwaighofer | e972d03 | 2013-05-05 01:54:46 +0000 | [diff] [blame] | 722 | } |
| 723 | |
Chandler Carruth | c77d50a | 2014-01-05 03:28:29 +0000 | [diff] [blame] | 724 | TEST_F(PatternMatchTest, OverflowingBinOps) { |
| 725 | Value *L = IRB.getInt32(1); |
| 726 | Value *R = IRB.getInt32(2); |
| 727 | Value *MatchL, *MatchR; |
| 728 | |
| 729 | EXPECT_TRUE( |
| 730 | m_NSWAdd(m_Value(MatchL), m_Value(MatchR)).match(IRB.CreateNSWAdd(L, R))); |
| 731 | EXPECT_EQ(L, MatchL); |
| 732 | EXPECT_EQ(R, MatchR); |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 733 | MatchL = MatchR = nullptr; |
Chandler Carruth | c77d50a | 2014-01-05 03:28:29 +0000 | [diff] [blame] | 734 | EXPECT_TRUE( |
| 735 | m_NSWSub(m_Value(MatchL), m_Value(MatchR)).match(IRB.CreateNSWSub(L, R))); |
| 736 | EXPECT_EQ(L, MatchL); |
| 737 | EXPECT_EQ(R, MatchR); |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 738 | MatchL = MatchR = nullptr; |
Chandler Carruth | c77d50a | 2014-01-05 03:28:29 +0000 | [diff] [blame] | 739 | EXPECT_TRUE( |
| 740 | m_NSWMul(m_Value(MatchL), m_Value(MatchR)).match(IRB.CreateNSWMul(L, R))); |
| 741 | EXPECT_EQ(L, MatchL); |
| 742 | EXPECT_EQ(R, MatchR); |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 743 | MatchL = MatchR = nullptr; |
Chandler Carruth | c77d50a | 2014-01-05 03:28:29 +0000 | [diff] [blame] | 744 | EXPECT_TRUE(m_NSWShl(m_Value(MatchL), m_Value(MatchR)).match( |
| 745 | IRB.CreateShl(L, R, "", /* NUW */ false, /* NSW */ true))); |
| 746 | EXPECT_EQ(L, MatchL); |
| 747 | EXPECT_EQ(R, MatchR); |
| 748 | |
| 749 | EXPECT_TRUE( |
| 750 | m_NUWAdd(m_Value(MatchL), m_Value(MatchR)).match(IRB.CreateNUWAdd(L, R))); |
| 751 | EXPECT_EQ(L, MatchL); |
| 752 | EXPECT_EQ(R, MatchR); |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 753 | MatchL = MatchR = nullptr; |
Chandler Carruth | c77d50a | 2014-01-05 03:28:29 +0000 | [diff] [blame] | 754 | EXPECT_TRUE( |
| 755 | m_NUWSub(m_Value(MatchL), m_Value(MatchR)).match(IRB.CreateNUWSub(L, R))); |
| 756 | EXPECT_EQ(L, MatchL); |
| 757 | EXPECT_EQ(R, MatchR); |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 758 | MatchL = MatchR = nullptr; |
Chandler Carruth | c77d50a | 2014-01-05 03:28:29 +0000 | [diff] [blame] | 759 | EXPECT_TRUE( |
| 760 | m_NUWMul(m_Value(MatchL), m_Value(MatchR)).match(IRB.CreateNUWMul(L, R))); |
| 761 | EXPECT_EQ(L, MatchL); |
| 762 | EXPECT_EQ(R, MatchR); |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 763 | MatchL = MatchR = nullptr; |
Chandler Carruth | c77d50a | 2014-01-05 03:28:29 +0000 | [diff] [blame] | 764 | EXPECT_TRUE(m_NUWShl(m_Value(MatchL), m_Value(MatchR)).match( |
| 765 | IRB.CreateShl(L, R, "", /* NUW */ true, /* NSW */ false))); |
| 766 | EXPECT_EQ(L, MatchL); |
| 767 | EXPECT_EQ(R, MatchR); |
| 768 | |
| 769 | EXPECT_FALSE(m_NSWAdd(m_Value(), m_Value()).match(IRB.CreateAdd(L, R))); |
| 770 | EXPECT_FALSE(m_NSWAdd(m_Value(), m_Value()).match(IRB.CreateNUWAdd(L, R))); |
| 771 | EXPECT_FALSE(m_NSWAdd(m_Value(), m_Value()).match(IRB.CreateNSWSub(L, R))); |
| 772 | EXPECT_FALSE(m_NSWSub(m_Value(), m_Value()).match(IRB.CreateSub(L, R))); |
| 773 | EXPECT_FALSE(m_NSWSub(m_Value(), m_Value()).match(IRB.CreateNUWSub(L, R))); |
| 774 | EXPECT_FALSE(m_NSWSub(m_Value(), m_Value()).match(IRB.CreateNSWAdd(L, R))); |
| 775 | EXPECT_FALSE(m_NSWMul(m_Value(), m_Value()).match(IRB.CreateMul(L, R))); |
| 776 | EXPECT_FALSE(m_NSWMul(m_Value(), m_Value()).match(IRB.CreateNUWMul(L, R))); |
| 777 | EXPECT_FALSE(m_NSWMul(m_Value(), m_Value()).match(IRB.CreateNSWAdd(L, R))); |
| 778 | EXPECT_FALSE(m_NSWShl(m_Value(), m_Value()).match(IRB.CreateShl(L, R))); |
| 779 | EXPECT_FALSE(m_NSWShl(m_Value(), m_Value()).match( |
| 780 | IRB.CreateShl(L, R, "", /* NUW */ true, /* NSW */ false))); |
| 781 | EXPECT_FALSE(m_NSWShl(m_Value(), m_Value()).match(IRB.CreateNSWAdd(L, R))); |
| 782 | |
| 783 | EXPECT_FALSE(m_NUWAdd(m_Value(), m_Value()).match(IRB.CreateAdd(L, R))); |
| 784 | EXPECT_FALSE(m_NUWAdd(m_Value(), m_Value()).match(IRB.CreateNSWAdd(L, R))); |
| 785 | EXPECT_FALSE(m_NUWAdd(m_Value(), m_Value()).match(IRB.CreateNUWSub(L, R))); |
| 786 | EXPECT_FALSE(m_NUWSub(m_Value(), m_Value()).match(IRB.CreateSub(L, R))); |
| 787 | EXPECT_FALSE(m_NUWSub(m_Value(), m_Value()).match(IRB.CreateNSWSub(L, R))); |
| 788 | EXPECT_FALSE(m_NUWSub(m_Value(), m_Value()).match(IRB.CreateNUWAdd(L, R))); |
| 789 | EXPECT_FALSE(m_NUWMul(m_Value(), m_Value()).match(IRB.CreateMul(L, R))); |
| 790 | EXPECT_FALSE(m_NUWMul(m_Value(), m_Value()).match(IRB.CreateNSWMul(L, R))); |
| 791 | EXPECT_FALSE(m_NUWMul(m_Value(), m_Value()).match(IRB.CreateNUWAdd(L, R))); |
| 792 | EXPECT_FALSE(m_NUWShl(m_Value(), m_Value()).match(IRB.CreateShl(L, R))); |
| 793 | EXPECT_FALSE(m_NUWShl(m_Value(), m_Value()).match( |
| 794 | IRB.CreateShl(L, R, "", /* NUW */ false, /* NSW */ true))); |
| 795 | EXPECT_FALSE(m_NUWShl(m_Value(), m_Value()).match(IRB.CreateNUWAdd(L, R))); |
| 796 | } |
| 797 | |
Sjoerd Meijer | c607901 | 2018-06-20 07:27:45 +0000 | [diff] [blame] | 798 | TEST_F(PatternMatchTest, LoadStoreOps) { |
| 799 | // Create this load/store sequence: |
| 800 | // |
| 801 | // %p = alloca i32* |
| 802 | // %0 = load i32*, i32** %p |
| 803 | // store i32 42, i32* %0 |
| 804 | |
| 805 | Value *Alloca = IRB.CreateAlloca(IRB.getInt32Ty()); |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame] | 806 | Value *LoadInst = IRB.CreateLoad(IRB.getInt32Ty(), Alloca); |
Sjoerd Meijer | c607901 | 2018-06-20 07:27:45 +0000 | [diff] [blame] | 807 | Value *FourtyTwo = IRB.getInt32(42); |
| 808 | Value *StoreInst = IRB.CreateStore(FourtyTwo, Alloca); |
| 809 | Value *MatchLoad, *MatchStoreVal, *MatchStorePointer; |
| 810 | |
| 811 | EXPECT_TRUE(m_Load(m_Value(MatchLoad)).match(LoadInst)); |
| 812 | EXPECT_EQ(Alloca, MatchLoad); |
| 813 | |
| 814 | EXPECT_TRUE(m_Load(m_Specific(Alloca)).match(LoadInst)); |
| 815 | |
| 816 | EXPECT_FALSE(m_Load(m_Value(MatchLoad)).match(Alloca)); |
| 817 | |
| 818 | EXPECT_TRUE(m_Store(m_Value(MatchStoreVal), m_Value(MatchStorePointer)) |
| 819 | .match(StoreInst)); |
| 820 | EXPECT_EQ(FourtyTwo, MatchStoreVal); |
| 821 | EXPECT_EQ(Alloca, MatchStorePointer); |
| 822 | |
| 823 | EXPECT_FALSE(m_Store(m_Value(MatchStoreVal), m_Value(MatchStorePointer)) |
| 824 | .match(Alloca)); |
| 825 | |
| 826 | EXPECT_TRUE(m_Store(m_SpecificInt(42), m_Specific(Alloca)) |
| 827 | .match(StoreInst)); |
| 828 | EXPECT_FALSE(m_Store(m_SpecificInt(42), m_Specific(FourtyTwo)) |
| 829 | .match(StoreInst)); |
| 830 | EXPECT_FALSE(m_Store(m_SpecificInt(43), m_Specific(Alloca)) |
| 831 | .match(StoreInst)); |
| 832 | } |
| 833 | |
Daniel Neilson | 45796f6 | 2018-03-28 15:39:00 +0000 | [diff] [blame] | 834 | TEST_F(PatternMatchTest, VectorOps) { |
| 835 | // Build up small tree of vector operations |
| 836 | // |
| 837 | // Val = 0 + 1 |
| 838 | // Val2 = Val + 3 |
| 839 | // VI1 = insertelement <2 x i8> undef, i8 1, i32 0 = <1, undef> |
| 840 | // VI2 = insertelement <2 x i8> %VI1, i8 %Val2, i8 %Val = <1, 4> |
| 841 | // VI3 = insertelement <2 x i8> %VI1, i8 %Val2, i32 1 = <1, 4> |
| 842 | // VI4 = insertelement <2 x i8> %VI1, i8 2, i8 %Val = <1, 2> |
| 843 | // |
| 844 | // SI1 = shufflevector <2 x i8> %VI1, <2 x i8> undef, zeroinitializer |
| 845 | // SI2 = shufflevector <2 x i8> %VI3, <2 x i8> %VI4, <2 x i8> <i8 0, i8 2> |
| 846 | // SI3 = shufflevector <2 x i8> %VI3, <2 x i8> undef, zeroinitializer |
| 847 | // SI4 = shufflevector <2 x i8> %VI4, <2 x i8> undef, zeroinitializer |
| 848 | // |
| 849 | // SP1 = VectorSplat(2, i8 2) |
| 850 | // SP2 = VectorSplat(2, i8 %Val) |
| 851 | Type *VecTy = VectorType::get(IRB.getInt8Ty(), 2); |
| 852 | Type *i32 = IRB.getInt32Ty(); |
| 853 | Type *i32VecTy = VectorType::get(i32, 2); |
| 854 | |
| 855 | Value *Val = IRB.CreateAdd(IRB.getInt8(0), IRB.getInt8(1)); |
| 856 | Value *Val2 = IRB.CreateAdd(Val, IRB.getInt8(3)); |
| 857 | |
| 858 | SmallVector<Constant *, 2> VecElemIdxs; |
| 859 | VecElemIdxs.push_back(ConstantInt::get(i32, 0)); |
| 860 | VecElemIdxs.push_back(ConstantInt::get(i32, 2)); |
| 861 | auto *IdxVec = ConstantVector::get(VecElemIdxs); |
| 862 | |
| 863 | Value *UndefVec = UndefValue::get(VecTy); |
| 864 | Value *VI1 = IRB.CreateInsertElement(UndefVec, IRB.getInt8(1), (uint64_t)0); |
| 865 | Value *VI2 = IRB.CreateInsertElement(VI1, Val2, Val); |
| 866 | Value *VI3 = IRB.CreateInsertElement(VI1, Val2, (uint64_t)1); |
| 867 | Value *VI4 = IRB.CreateInsertElement(VI1, IRB.getInt8(2), Val); |
| 868 | |
| 869 | Value *EX1 = IRB.CreateExtractElement(VI4, Val); |
| 870 | Value *EX2 = IRB.CreateExtractElement(VI4, (uint64_t)0); |
| 871 | Value *EX3 = IRB.CreateExtractElement(IdxVec, (uint64_t)1); |
| 872 | |
| 873 | Value *Zero = ConstantAggregateZero::get(i32VecTy); |
| 874 | Value *SI1 = IRB.CreateShuffleVector(VI1, UndefVec, Zero); |
| 875 | Value *SI2 = IRB.CreateShuffleVector(VI3, VI4, IdxVec); |
| 876 | Value *SI3 = IRB.CreateShuffleVector(VI3, UndefVec, Zero); |
| 877 | Value *SI4 = IRB.CreateShuffleVector(VI4, UndefVec, Zero); |
| 878 | |
| 879 | Value *SP1 = IRB.CreateVectorSplat(2, IRB.getInt8(2)); |
| 880 | Value *SP2 = IRB.CreateVectorSplat(2, Val); |
| 881 | |
| 882 | Value *A = nullptr, *B = nullptr, *C = nullptr; |
| 883 | |
| 884 | // Test matching insertelement |
| 885 | EXPECT_TRUE(match(VI1, m_InsertElement(m_Value(), m_Value(), m_Value()))); |
| 886 | EXPECT_TRUE( |
| 887 | match(VI1, m_InsertElement(m_Undef(), m_ConstantInt(), m_ConstantInt()))); |
| 888 | EXPECT_TRUE( |
| 889 | match(VI1, m_InsertElement(m_Undef(), m_ConstantInt(), m_Zero()))); |
| 890 | EXPECT_TRUE( |
| 891 | match(VI1, m_InsertElement(m_Undef(), m_SpecificInt(1), m_Zero()))); |
| 892 | EXPECT_TRUE(match(VI2, m_InsertElement(m_Value(), m_Value(), m_Value()))); |
| 893 | EXPECT_FALSE( |
| 894 | match(VI2, m_InsertElement(m_Value(), m_Value(), m_ConstantInt()))); |
| 895 | EXPECT_FALSE( |
| 896 | match(VI2, m_InsertElement(m_Value(), m_ConstantInt(), m_Value()))); |
| 897 | EXPECT_FALSE(match(VI2, m_InsertElement(m_Constant(), m_Value(), m_Value()))); |
| 898 | EXPECT_TRUE(match(VI3, m_InsertElement(m_Value(A), m_Value(B), m_Value(C)))); |
| 899 | EXPECT_TRUE(A == VI1); |
| 900 | EXPECT_TRUE(B == Val2); |
| 901 | EXPECT_TRUE(isa<ConstantInt>(C)); |
| 902 | A = B = C = nullptr; // reset |
| 903 | |
| 904 | // Test matching extractelement |
| 905 | EXPECT_TRUE(match(EX1, m_ExtractElement(m_Value(A), m_Value(B)))); |
| 906 | EXPECT_TRUE(A == VI4); |
| 907 | EXPECT_TRUE(B == Val); |
| 908 | A = B = C = nullptr; // reset |
| 909 | EXPECT_FALSE(match(EX1, m_ExtractElement(m_Value(), m_ConstantInt()))); |
| 910 | EXPECT_TRUE(match(EX2, m_ExtractElement(m_Value(), m_ConstantInt()))); |
| 911 | EXPECT_TRUE(match(EX3, m_ExtractElement(m_Constant(), m_ConstantInt()))); |
| 912 | |
| 913 | // Test matching shufflevector |
| 914 | EXPECT_TRUE(match(SI1, m_ShuffleVector(m_Value(), m_Undef(), m_Zero()))); |
| 915 | EXPECT_TRUE(match(SI2, m_ShuffleVector(m_Value(A), m_Value(B), m_Value(C)))); |
| 916 | EXPECT_TRUE(A == VI3); |
| 917 | EXPECT_TRUE(B == VI4); |
| 918 | EXPECT_TRUE(C == IdxVec); |
| 919 | A = B = C = nullptr; // reset |
| 920 | |
| 921 | // Test matching the vector splat pattern |
| 922 | EXPECT_TRUE(match( |
| 923 | SI1, |
| 924 | m_ShuffleVector(m_InsertElement(m_Undef(), m_SpecificInt(1), m_Zero()), |
| 925 | m_Undef(), m_Zero()))); |
| 926 | EXPECT_FALSE(match( |
| 927 | SI3, m_ShuffleVector(m_InsertElement(m_Undef(), m_Value(), m_Zero()), |
| 928 | m_Undef(), m_Zero()))); |
| 929 | EXPECT_FALSE(match( |
| 930 | SI4, m_ShuffleVector(m_InsertElement(m_Undef(), m_Value(), m_Zero()), |
| 931 | m_Undef(), m_Zero()))); |
| 932 | EXPECT_TRUE(match( |
| 933 | SP1, |
| 934 | m_ShuffleVector(m_InsertElement(m_Undef(), m_SpecificInt(2), m_Zero()), |
| 935 | m_Undef(), m_Zero()))); |
| 936 | EXPECT_TRUE(match( |
| 937 | SP2, m_ShuffleVector(m_InsertElement(m_Undef(), m_Value(A), m_Zero()), |
| 938 | m_Undef(), m_Zero()))); |
| 939 | EXPECT_TRUE(A == Val); |
| 940 | } |
| 941 | |
Sanjay Patel | f5ead29 | 2018-11-20 16:08:19 +0000 | [diff] [blame] | 942 | TEST_F(PatternMatchTest, VectorUndefInt) { |
| 943 | Type *ScalarTy = IRB.getInt8Ty(); |
| 944 | Type *VectorTy = VectorType::get(ScalarTy, 4); |
| 945 | Constant *ScalarUndef = UndefValue::get(ScalarTy); |
| 946 | Constant *VectorUndef = UndefValue::get(VectorTy); |
| 947 | Constant *ScalarZero = Constant::getNullValue(ScalarTy); |
| 948 | Constant *VectorZero = Constant::getNullValue(VectorTy); |
| 949 | |
| 950 | SmallVector<Constant *, 4> Elems; |
| 951 | Elems.push_back(ScalarUndef); |
| 952 | Elems.push_back(ScalarZero); |
| 953 | Elems.push_back(ScalarUndef); |
| 954 | Elems.push_back(ScalarZero); |
| 955 | Constant *VectorZeroUndef = ConstantVector::get(Elems); |
| 956 | |
| 957 | EXPECT_TRUE(match(ScalarUndef, m_Undef())); |
| 958 | EXPECT_TRUE(match(VectorUndef, m_Undef())); |
| 959 | EXPECT_FALSE(match(ScalarZero, m_Undef())); |
| 960 | EXPECT_FALSE(match(VectorZero, m_Undef())); |
| 961 | EXPECT_FALSE(match(VectorZeroUndef, m_Undef())); |
| 962 | |
| 963 | EXPECT_FALSE(match(ScalarUndef, m_Zero())); |
| 964 | EXPECT_FALSE(match(VectorUndef, m_Zero())); |
| 965 | EXPECT_TRUE(match(ScalarZero, m_Zero())); |
| 966 | EXPECT_TRUE(match(VectorZero, m_Zero())); |
| 967 | EXPECT_TRUE(match(VectorZeroUndef, m_Zero())); |
| 968 | } |
| 969 | |
| 970 | TEST_F(PatternMatchTest, VectorUndefFloat) { |
| 971 | Type *ScalarTy = IRB.getFloatTy(); |
| 972 | Type *VectorTy = VectorType::get(ScalarTy, 4); |
| 973 | Constant *ScalarUndef = UndefValue::get(ScalarTy); |
| 974 | Constant *VectorUndef = UndefValue::get(VectorTy); |
| 975 | Constant *ScalarZero = Constant::getNullValue(ScalarTy); |
| 976 | Constant *VectorZero = Constant::getNullValue(VectorTy); |
| 977 | |
| 978 | SmallVector<Constant *, 4> Elems; |
| 979 | Elems.push_back(ScalarUndef); |
| 980 | Elems.push_back(ScalarZero); |
| 981 | Elems.push_back(ScalarUndef); |
| 982 | Elems.push_back(ScalarZero); |
| 983 | Constant *VectorZeroUndef = ConstantVector::get(Elems); |
| 984 | |
| 985 | EXPECT_TRUE(match(ScalarUndef, m_Undef())); |
| 986 | EXPECT_TRUE(match(VectorUndef, m_Undef())); |
| 987 | EXPECT_FALSE(match(ScalarZero, m_Undef())); |
| 988 | EXPECT_FALSE(match(VectorZero, m_Undef())); |
| 989 | EXPECT_FALSE(match(VectorZeroUndef, m_Undef())); |
| 990 | |
| 991 | EXPECT_FALSE(match(ScalarUndef, m_AnyZeroFP())); |
| 992 | EXPECT_FALSE(match(VectorUndef, m_AnyZeroFP())); |
| 993 | EXPECT_TRUE(match(ScalarZero, m_AnyZeroFP())); |
| 994 | EXPECT_TRUE(match(VectorZero, m_AnyZeroFP())); |
| 995 | EXPECT_TRUE(match(VectorZeroUndef, m_AnyZeroFP())); |
| 996 | } |
| 997 | |
Cameron McInally | be7138b | 2019-05-03 21:19:12 +0000 | [diff] [blame] | 998 | TEST_F(PatternMatchTest, FloatingPointFNeg) { |
| 999 | Type *FltTy = IRB.getFloatTy(); |
| 1000 | Value *One = ConstantFP::get(FltTy, 1.0); |
| 1001 | Value *Z = ConstantFP::get(FltTy, 0.0); |
| 1002 | Value *NZ = ConstantFP::get(FltTy, -0.0); |
| 1003 | Value *V = IRB.CreateFNeg(One); |
| 1004 | Value *V1 = IRB.CreateFSub(NZ, One); |
| 1005 | Value *V2 = IRB.CreateFSub(Z, One); |
| 1006 | Value *V3 = IRB.CreateFAdd(NZ, One); |
| 1007 | Value *Match; |
| 1008 | |
| 1009 | // Test FNeg(1.0) |
| 1010 | EXPECT_TRUE(match(V, m_FNeg(m_Value(Match)))); |
| 1011 | EXPECT_EQ(One, Match); |
| 1012 | |
| 1013 | // Test FSub(-0.0, 1.0) |
| 1014 | EXPECT_TRUE(match(V1, m_FNeg(m_Value(Match)))); |
| 1015 | EXPECT_EQ(One, Match); |
| 1016 | |
| 1017 | // Test FSub(0.0, 1.0) |
| 1018 | EXPECT_FALSE(match(V2, m_FNeg(m_Value(Match)))); |
| 1019 | cast<Instruction>(V2)->setHasNoSignedZeros(true); |
| 1020 | EXPECT_TRUE(match(V2, m_FNeg(m_Value(Match)))); |
| 1021 | EXPECT_EQ(One, Match); |
| 1022 | |
| 1023 | // Test FAdd(-0.0, 1.0) |
| 1024 | EXPECT_FALSE(match(V3, m_FNeg(m_Value(Match)))); |
| 1025 | } |
| 1026 | |
Pete Cooper | ab47fa6 | 2016-08-12 22:16:05 +0000 | [diff] [blame] | 1027 | template <typename T> struct MutableConstTest : PatternMatchTest { }; |
| 1028 | |
| 1029 | typedef ::testing::Types<std::tuple<Value*, Instruction*>, |
| 1030 | std::tuple<const Value*, const Instruction *>> |
| 1031 | MutableConstTestTypes; |
| 1032 | TYPED_TEST_CASE(MutableConstTest, MutableConstTestTypes); |
| 1033 | |
| 1034 | TYPED_TEST(MutableConstTest, ICmp) { |
| 1035 | auto &IRB = PatternMatchTest::IRB; |
| 1036 | |
| 1037 | typedef typename std::tuple_element<0, TypeParam>::type ValueType; |
| 1038 | typedef typename std::tuple_element<1, TypeParam>::type InstructionType; |
| 1039 | |
| 1040 | Value *L = IRB.getInt32(1); |
| 1041 | Value *R = IRB.getInt32(2); |
| 1042 | ICmpInst::Predicate Pred = ICmpInst::ICMP_UGT; |
| 1043 | |
| 1044 | ValueType MatchL; |
| 1045 | ValueType MatchR; |
| 1046 | ICmpInst::Predicate MatchPred; |
| 1047 | |
| 1048 | EXPECT_TRUE(m_ICmp(MatchPred, m_Value(MatchL), m_Value(MatchR)) |
| 1049 | .match((InstructionType)IRB.CreateICmp(Pred, L, R))); |
| 1050 | EXPECT_EQ(L, MatchL); |
| 1051 | EXPECT_EQ(R, MatchR); |
| 1052 | } |
| 1053 | |
Arnold Schwaighofer | e972d03 | 2013-05-05 01:54:46 +0000 | [diff] [blame] | 1054 | } // anonymous namespace. |