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