| 1 |
//===---- llvm/unittest/IR/PatternMatch.cpp - PatternMatch unit tests ----===// |
| 2 |
// |
| 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 |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 9 |
#include "llvm/IR/PatternMatch.h" |
| 10 |
#include "llvm/ADT/APSInt.h" |
| 11 |
#include "llvm/ADT/STLExtras.h" |
| 12 |
#include "llvm/Analysis/ValueTracking.h" |
| 13 |
#include "llvm/IR/BasicBlock.h" |
| 14 |
#include "llvm/IR/Constants.h" |
| 15 |
#include "llvm/IR/DataLayout.h" |
| 16 |
#include "llvm/IR/DerivedTypes.h" |
| 17 |
#include "llvm/IR/Function.h" |
| 18 |
#include "llvm/IR/IRBuilder.h" |
| 19 |
#include "llvm/IR/Instructions.h" |
| 20 |
#include "llvm/IR/LLVMContext.h" |
| 21 |
#include "llvm/IR/MDBuilder.h" |
| 22 |
#include "llvm/IR/Module.h" |
| 23 |
#include "llvm/IR/NoFolder.h" |
| 24 |
#include "llvm/IR/Operator.h" |
| 25 |
#include "llvm/IR/Type.h" |
| 26 |
#include "gtest/gtest.h" |
| 27 |
|
| 28 |
using namespace llvm; |
| 29 |
using namespace llvm::PatternMatch; |
| 30 |
|
| 31 |
namespace { |
| 32 |
|
| 33 |
struct PatternMatchTest : ::testing::Test { |
| 34 |
LLVMContext Ctx; |
| 35 |
std::unique_ptr<Module> M; |
| 36 |
Function *F; |
| 37 |
BasicBlock *BB; |
| 38 |
IRBuilder<NoFolder> IRB; |
| 39 |
|
| 40 |
PatternMatchTest() |
| 41 |
: M(new Module("PatternMatchTestModule", Ctx)), |
| 42 |
F(Function::Create( |
| 43 |
FunctionType::get(Type::getVoidTy(Ctx), /* IsVarArg */ false), |
| 44 |
Function::ExternalLinkage, "f", M.get())), |
| 45 |
BB(BasicBlock::Create(Ctx, "entry", F)), IRB(BB) {} |
| 46 |
}; |
| 47 |
|
| 48 |
TEST_F(PatternMatchTest, OneUse) { |
| 49 |
// Build up a little tree of values: |
| 50 |
// |
| 51 |
// One = (1 + 2) + 42 |
| 52 |
// Two = One + 42 |
| 53 |
// Leaf = (Two + 8) + (Two + 13) |
| 54 |
Value *One = IRB.CreateAdd(IRB.CreateAdd(IRB.getInt32(1), IRB.getInt32(2)), |
| 55 |
IRB.getInt32(42)); |
| 56 |
Value *Two = IRB.CreateAdd(One, IRB.getInt32(42)); |
| 57 |
Value *Leaf = IRB.CreateAdd(IRB.CreateAdd(Two, IRB.getInt32(8)), |
| 58 |
IRB.CreateAdd(Two, IRB.getInt32(13))); |
| 59 |
Value *V; |
| 60 |
|
| 61 |
EXPECT_TRUE(m_OneUse(m_Value(V)).match(One)); |
| 62 |
EXPECT_EQ(One, V); |
| 63 |
|
| 64 |
EXPECT_FALSE(m_OneUse(m_Value()).match(Two)); |
| 65 |
EXPECT_FALSE(m_OneUse(m_Value()).match(Leaf)); |
| 66 |
} |
| 67 |
|
| 68 |
TEST_F(PatternMatchTest, SpecificIntEQ) { |
| 69 |
Type *IntTy = IRB.getInt32Ty(); |
| 70 |
unsigned BitWidth = IntTy->getScalarSizeInBits(); |
| 71 |
|
| 72 |
Value *Zero = ConstantInt::get(IntTy, 0); |
| 73 |
Value *One = ConstantInt::get(IntTy, 1); |
| 74 |
Value *NegOne = ConstantInt::get(IntTy, -1); |
| 75 |
|
| 76 |
EXPECT_TRUE( |
| 77 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, 0)) |
| 78 |
.match(Zero)); |
| 79 |
EXPECT_FALSE( |
| 80 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, 0)) |
| 81 |
.match(One)); |
| 82 |
EXPECT_FALSE( |
| 83 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, 0)) |
| 84 |
.match(NegOne)); |
| 85 |
|
| 86 |
EXPECT_FALSE( |
| 87 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, 1)) |
| 88 |
.match(Zero)); |
| 89 |
EXPECT_TRUE( |
| 90 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, 1)) |
| 91 |
.match(One)); |
| 92 |
EXPECT_FALSE( |
| 93 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, 1)) |
| 94 |
.match(NegOne)); |
| 95 |
|
| 96 |
EXPECT_FALSE( |
| 97 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, -1)) |
| 98 |
.match(Zero)); |
| 99 |
EXPECT_FALSE( |
| 100 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, -1)) |
| 101 |
.match(One)); |
| 102 |
EXPECT_TRUE( |
| 103 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, APInt(BitWidth, -1)) |
| 104 |
.match(NegOne)); |
| 105 |
} |
| 106 |
|
| 107 |
TEST_F(PatternMatchTest, SpecificIntNE) { |
| 108 |
Type *IntTy = IRB.getInt32Ty(); |
| 109 |
unsigned BitWidth = IntTy->getScalarSizeInBits(); |
| 110 |
|
| 111 |
Value *Zero = ConstantInt::get(IntTy, 0); |
| 112 |
Value *One = ConstantInt::get(IntTy, 1); |
| 113 |
Value *NegOne = ConstantInt::get(IntTy, -1); |
| 114 |
|
| 115 |
EXPECT_FALSE( |
| 116 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, 0)) |
| 117 |
.match(Zero)); |
| 118 |
EXPECT_TRUE( |
| 119 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, 0)) |
| 120 |
.match(One)); |
| 121 |
EXPECT_TRUE( |
| 122 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, 0)) |
| 123 |
.match(NegOne)); |
| 124 |
|
| 125 |
EXPECT_TRUE( |
| 126 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, 1)) |
| 127 |
.match(Zero)); |
| 128 |
EXPECT_FALSE( |
| 129 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, 1)) |
| 130 |
.match(One)); |
| 131 |
EXPECT_TRUE( |
| 132 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, 1)) |
| 133 |
.match(NegOne)); |
| 134 |
|
| 135 |
EXPECT_TRUE( |
| 136 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, -1)) |
| 137 |
.match(Zero)); |
| 138 |
EXPECT_TRUE( |
| 139 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, -1)) |
| 140 |
.match(One)); |
| 141 |
EXPECT_FALSE( |
| 142 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE, APInt(BitWidth, -1)) |
| 143 |
.match(NegOne)); |
| 144 |
} |
| 145 |
|
| 146 |
TEST_F(PatternMatchTest, SpecificIntUGT) { |
| 147 |
Type *IntTy = IRB.getInt32Ty(); |
| 148 |
unsigned BitWidth = IntTy->getScalarSizeInBits(); |
| 149 |
|
| 150 |
Value *Zero = ConstantInt::get(IntTy, 0); |
| 151 |
Value *One = ConstantInt::get(IntTy, 1); |
| 152 |
Value *NegOne = ConstantInt::get(IntTy, -1); |
| 153 |
|
| 154 |
EXPECT_FALSE( |
| 155 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, 0)) |
| 156 |
.match(Zero)); |
| 157 |
EXPECT_TRUE( |
| 158 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, 0)) |
| 159 |
.match(One)); |
| 160 |
EXPECT_TRUE( |
| 161 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, 0)) |
| 162 |
.match(NegOne)); |
| 163 |
|
| 164 |
EXPECT_FALSE( |
| 165 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, 1)) |
| 166 |
.match(Zero)); |
| 167 |
EXPECT_FALSE( |
| 168 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, 1)) |
| 169 |
.match(One)); |
| 170 |
EXPECT_TRUE( |
| 171 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, 1)) |
| 172 |
.match(NegOne)); |
| 173 |
|
| 174 |
EXPECT_FALSE( |
| 175 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, -1)) |
| 176 |
.match(Zero)); |
| 177 |
EXPECT_FALSE( |
| 178 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, -1)) |
| 179 |
.match(One)); |
| 180 |
EXPECT_FALSE( |
| 181 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGT, APInt(BitWidth, -1)) |
| 182 |
.match(NegOne)); |
| 183 |
} |
| 184 |
|
| 185 |
TEST_F(PatternMatchTest, SignbitZeroChecks) { |
| 186 |
Type *IntTy = IRB.getInt32Ty(); |
| 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(m_Negative().match(NegOne)); |
| 193 |
EXPECT_FALSE(m_NonNegative().match(NegOne)); |
| 194 |
EXPECT_FALSE(m_StrictlyPositive().match(NegOne)); |
| 195 |
EXPECT_TRUE(m_NonPositive().match(NegOne)); |
| 196 |
|
| 197 |
EXPECT_FALSE(m_Negative().match(Zero)); |
| 198 |
EXPECT_TRUE(m_NonNegative().match(Zero)); |
| 199 |
EXPECT_FALSE(m_StrictlyPositive().match(Zero)); |
| 200 |
EXPECT_TRUE(m_NonPositive().match(Zero)); |
| 201 |
|
| 202 |
EXPECT_FALSE(m_Negative().match(One)); |
| 203 |
EXPECT_TRUE(m_NonNegative().match(One)); |
| 204 |
EXPECT_TRUE(m_StrictlyPositive().match(One)); |
| 205 |
EXPECT_FALSE(m_NonPositive().match(One)); |
| 206 |
} |
| 207 |
|
| 208 |
TEST_F(PatternMatchTest, SpecificIntUGE) { |
| 209 |
Type *IntTy = IRB.getInt32Ty(); |
| 210 |
unsigned BitWidth = IntTy->getScalarSizeInBits(); |
| 211 |
|
| 212 |
Value *Zero = ConstantInt::get(IntTy, 0); |
| 213 |
Value *One = ConstantInt::get(IntTy, 1); |
| 214 |
Value *NegOne = ConstantInt::get(IntTy, -1); |
| 215 |
|
| 216 |
EXPECT_TRUE( |
| 217 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, 0)) |
| 218 |
.match(Zero)); |
| 219 |
EXPECT_TRUE( |
| 220 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, 0)) |
| 221 |
.match(One)); |
| 222 |
EXPECT_TRUE( |
| 223 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, 0)) |
| 224 |
.match(NegOne)); |
| 225 |
|
| 226 |
EXPECT_FALSE( |
| 227 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, 1)) |
| 228 |
.match(Zero)); |
| 229 |
EXPECT_TRUE( |
| 230 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, 1)) |
| 231 |
.match(One)); |
| 232 |
EXPECT_TRUE( |
| 233 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, 1)) |
| 234 |
.match(NegOne)); |
| 235 |
|
| 236 |
EXPECT_FALSE( |
| 237 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, -1)) |
| 238 |
.match(Zero)); |
| 239 |
EXPECT_FALSE( |
| 240 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, -1)) |
| 241 |
.match(One)); |
| 242 |
EXPECT_TRUE( |
| 243 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE, APInt(BitWidth, -1)) |
| 244 |
.match(NegOne)); |
| 245 |
} |
| 246 |
|
| 247 |
TEST_F(PatternMatchTest, SpecificIntULT) { |
| 248 |
Type *IntTy = IRB.getInt32Ty(); |
| 249 |
unsigned BitWidth = IntTy->getScalarSizeInBits(); |
| 250 |
|
| 251 |
Value *Zero = ConstantInt::get(IntTy, 0); |
| 252 |
Value *One = ConstantInt::get(IntTy, 1); |
| 253 |
Value *NegOne = ConstantInt::get(IntTy, -1); |
| 254 |
|
| 255 |
EXPECT_FALSE( |
| 256 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, 0)) |
| 257 |
.match(Zero)); |
| 258 |
EXPECT_FALSE( |
| 259 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, 0)) |
| 260 |
.match(One)); |
| 261 |
EXPECT_FALSE( |
| 262 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, 0)) |
| 263 |
.match(NegOne)); |
| 264 |
|
| 265 |
EXPECT_TRUE( |
| 266 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, 1)) |
| 267 |
.match(Zero)); |
| 268 |
EXPECT_FALSE( |
| 269 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, 1)) |
| 270 |
.match(One)); |
| 271 |
EXPECT_FALSE( |
| 272 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, 1)) |
| 273 |
.match(NegOne)); |
| 274 |
|
| 275 |
EXPECT_TRUE( |
| 276 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, -1)) |
| 277 |
.match(Zero)); |
| 278 |
EXPECT_TRUE( |
| 279 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, -1)) |
| 280 |
.match(One)); |
| 281 |
EXPECT_FALSE( |
| 282 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, APInt(BitWidth, -1)) |
| 283 |
.match(NegOne)); |
| 284 |
} |
| 285 |
|
| 286 |
TEST_F(PatternMatchTest, SpecificIntULE) { |
| 287 |
Type *IntTy = IRB.getInt32Ty(); |
| 288 |
unsigned BitWidth = IntTy->getScalarSizeInBits(); |
| 289 |
|
| 290 |
Value *Zero = ConstantInt::get(IntTy, 0); |
| 291 |
Value *One = ConstantInt::get(IntTy, 1); |
| 292 |
Value *NegOne = ConstantInt::get(IntTy, -1); |
| 293 |
|
| 294 |
EXPECT_TRUE( |
| 295 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, 0)) |
| 296 |
.match(Zero)); |
| 297 |
EXPECT_FALSE( |
| 298 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, 0)) |
| 299 |
.match(One)); |
| 300 |
EXPECT_FALSE( |
| 301 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, 0)) |
| 302 |
.match(NegOne)); |
| 303 |
|
| 304 |
EXPECT_TRUE( |
| 305 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, 1)) |
| 306 |
.match(Zero)); |
| 307 |
EXPECT_TRUE( |
| 308 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, 1)) |
| 309 |
.match(One)); |
| 310 |
EXPECT_FALSE( |
| 311 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, 1)) |
| 312 |
.match(NegOne)); |
| 313 |
|
| 314 |
EXPECT_TRUE( |
| 315 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, -1)) |
| 316 |
.match(Zero)); |
| 317 |
EXPECT_TRUE( |
| 318 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, -1)) |
| 319 |
.match(One)); |
| 320 |
EXPECT_TRUE( |
| 321 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULE, APInt(BitWidth, -1)) |
| 322 |
.match(NegOne)); |
| 323 |
} |
| 324 |
|
| 325 |
TEST_F(PatternMatchTest, SpecificIntSGT) { |
| 326 |
Type *IntTy = IRB.getInt32Ty(); |
| 327 |
unsigned BitWidth = IntTy->getScalarSizeInBits(); |
| 328 |
|
| 329 |
Value *Zero = ConstantInt::get(IntTy, 0); |
| 330 |
Value *One = ConstantInt::get(IntTy, 1); |
| 331 |
Value *NegOne = ConstantInt::get(IntTy, -1); |
| 332 |
|
| 333 |
EXPECT_FALSE( |
| 334 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, 0)) |
| 335 |
.match(Zero)); |
| 336 |
EXPECT_TRUE( |
| 337 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, 0)) |
| 338 |
.match(One)); |
| 339 |
EXPECT_FALSE( |
| 340 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, 0)) |
| 341 |
.match(NegOne)); |
| 342 |
|
| 343 |
EXPECT_FALSE( |
| 344 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, 1)) |
| 345 |
.match(Zero)); |
| 346 |
EXPECT_FALSE( |
| 347 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, 1)) |
| 348 |
.match(One)); |
| 349 |
EXPECT_FALSE( |
| 350 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, 1)) |
| 351 |
.match(NegOne)); |
| 352 |
|
| 353 |
EXPECT_TRUE( |
| 354 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, -1)) |
| 355 |
.match(Zero)); |
| 356 |
EXPECT_TRUE( |
| 357 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, -1)) |
| 358 |
.match(One)); |
| 359 |
EXPECT_FALSE( |
| 360 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGT, APInt(BitWidth, -1)) |
| 361 |
.match(NegOne)); |
| 362 |
} |
| 363 |
|
| 364 |
TEST_F(PatternMatchTest, SpecificIntSGE) { |
| 365 |
Type *IntTy = IRB.getInt32Ty(); |
| 366 |
unsigned BitWidth = IntTy->getScalarSizeInBits(); |
| 367 |
|
| 368 |
Value *Zero = ConstantInt::get(IntTy, 0); |
| 369 |
Value *One = ConstantInt::get(IntTy, 1); |
| 370 |
Value *NegOne = ConstantInt::get(IntTy, -1); |
| 371 |
|
| 372 |
EXPECT_TRUE( |
| 373 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, 0)) |
| 374 |
.match(Zero)); |
| 375 |
EXPECT_TRUE( |
| 376 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, 0)) |
| 377 |
.match(One)); |
| 378 |
EXPECT_FALSE( |
| 379 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, 0)) |
| 380 |
.match(NegOne)); |
| 381 |
|
| 382 |
EXPECT_FALSE( |
| 383 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, 1)) |
| 384 |
.match(Zero)); |
| 385 |
EXPECT_TRUE( |
| 386 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, 1)) |
| 387 |
.match(One)); |
| 388 |
EXPECT_FALSE( |
| 389 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, 1)) |
| 390 |
.match(NegOne)); |
| 391 |
|
| 392 |
EXPECT_TRUE( |
| 393 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, -1)) |
| 394 |
.match(Zero)); |
| 395 |
EXPECT_TRUE( |
| 396 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, -1)) |
| 397 |
.match(One)); |
| 398 |
EXPECT_TRUE( |
| 399 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SGE, APInt(BitWidth, -1)) |
| 400 |
.match(NegOne)); |
| 401 |
} |
| 402 |
|
| 403 |
TEST_F(PatternMatchTest, SpecificIntSLT) { |
| 404 |
Type *IntTy = IRB.getInt32Ty(); |
| 405 |
unsigned BitWidth = IntTy->getScalarSizeInBits(); |
| 406 |
|
| 407 |
Value *Zero = ConstantInt::get(IntTy, 0); |
| 408 |
Value *One = ConstantInt::get(IntTy, 1); |
| 409 |
Value *NegOne = ConstantInt::get(IntTy, -1); |
| 410 |
|
| 411 |
EXPECT_FALSE( |
| 412 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, 0)) |
| 413 |
.match(Zero)); |
| 414 |
EXPECT_FALSE( |
| 415 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, 0)) |
| 416 |
.match(One)); |
| 417 |
EXPECT_TRUE( |
| 418 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, 0)) |
| 419 |
.match(NegOne)); |
| 420 |
|
| 421 |
EXPECT_TRUE( |
| 422 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, 1)) |
| 423 |
.match(Zero)); |
| 424 |
EXPECT_FALSE( |
| 425 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, 1)) |
| 426 |
.match(One)); |
| 427 |
EXPECT_TRUE( |
| 428 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, 1)) |
| 429 |
.match(NegOne)); |
| 430 |
|
| 431 |
EXPECT_FALSE( |
| 432 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, -1)) |
| 433 |
.match(Zero)); |
| 434 |
EXPECT_FALSE( |
| 435 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, -1)) |
| 436 |
.match(One)); |
| 437 |
EXPECT_FALSE( |
| 438 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLT, APInt(BitWidth, -1)) |
| 439 |
.match(NegOne)); |
| 440 |
} |
| 441 |
|
| 442 |
TEST_F(PatternMatchTest, SpecificIntSLE) { |
| 443 |
Type *IntTy = IRB.getInt32Ty(); |
| 444 |
unsigned BitWidth = IntTy->getScalarSizeInBits(); |
| 445 |
|
| 446 |
Value *Zero = ConstantInt::get(IntTy, 0); |
| 447 |
Value *One = ConstantInt::get(IntTy, 1); |
| 448 |
Value *NegOne = ConstantInt::get(IntTy, -1); |
| 449 |
|
| 450 |
EXPECT_TRUE( |
| 451 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, 0)) |
| 452 |
.match(Zero)); |
| 453 |
EXPECT_FALSE( |
| 454 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, 0)) |
| 455 |
.match(One)); |
| 456 |
EXPECT_TRUE( |
| 457 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, 0)) |
| 458 |
.match(NegOne)); |
| 459 |
|
| 460 |
EXPECT_TRUE( |
| 461 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, 1)) |
| 462 |
.match(Zero)); |
| 463 |
EXPECT_TRUE( |
| 464 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, 1)) |
| 465 |
.match(One)); |
| 466 |
EXPECT_TRUE( |
| 467 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, 1)) |
| 468 |
.match(NegOne)); |
| 469 |
|
| 470 |
EXPECT_FALSE( |
| 471 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, -1)) |
| 472 |
.match(Zero)); |
| 473 |
EXPECT_FALSE( |
| 474 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, -1)) |
| 475 |
.match(One)); |
| 476 |
EXPECT_TRUE( |
| 477 |
m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_SLE, APInt(BitWidth, -1)) |
| 478 |
.match(NegOne)); |
| 479 |
} |
| 480 |
|
| 481 |
TEST_F(PatternMatchTest, Unless) { |
| 482 |
Value *X = IRB.CreateAdd(IRB.getInt32(1), IRB.getInt32(0)); |
| 483 |
|
| 484 |
EXPECT_TRUE(m_Add(m_One(), m_Zero()).match(X)); |
| 485 |
EXPECT_FALSE(m_Add(m_Zero(), m_One()).match(X)); |
| 486 |
|
| 487 |
EXPECT_FALSE(m_Unless(m_Add(m_One(), m_Zero())).match(X)); |
| 488 |
EXPECT_TRUE(m_Unless(m_Add(m_Zero(), m_One())).match(X)); |
| 489 |
|
| 490 |
EXPECT_TRUE(m_c_Add(m_One(), m_Zero()).match(X)); |
| 491 |
EXPECT_TRUE(m_c_Add(m_Zero(), m_One()).match(X)); |
| 492 |
|
| 493 |
EXPECT_FALSE(m_Unless(m_c_Add(m_One(), m_Zero())).match(X)); |
| 494 |
EXPECT_FALSE(m_Unless(m_c_Add(m_Zero(), m_One())).match(X)); |
| 495 |
} |
| 496 |
|
| 497 |
TEST_F(PatternMatchTest, ZExtSExtSelf) { |
| 498 |
LLVMContext &Ctx = IRB.getContext(); |
| 499 |
|
| 500 |
Value *One32 = IRB.getInt32(1); |
| 501 |
Value *One64Z = IRB.CreateZExt(One32, IntegerType::getInt64Ty(Ctx)); |
| 502 |
Value *One64S = IRB.CreateSExt(One32, IntegerType::getInt64Ty(Ctx)); |
| 503 |
|
| 504 |
EXPECT_TRUE(m_One().match(One32)); |
| 505 |
EXPECT_FALSE(m_One().match(One64Z)); |
| 506 |
EXPECT_FALSE(m_One().match(One64S)); |
| 507 |
|
| 508 |
EXPECT_FALSE(m_ZExt(m_One()).match(One32)); |
| 509 |
EXPECT_TRUE(m_ZExt(m_One()).match(One64Z)); |
| 510 |
EXPECT_FALSE(m_ZExt(m_One()).match(One64S)); |
| 511 |
|
| 512 |
EXPECT_FALSE(m_SExt(m_One()).match(One32)); |
| 513 |
EXPECT_FALSE(m_SExt(m_One()).match(One64Z)); |
| 514 |
EXPECT_TRUE(m_SExt(m_One()).match(One64S)); |
| 515 |
|
| 516 |
EXPECT_TRUE(m_ZExtOrSelf(m_One()).match(One32)); |
| 517 |
EXPECT_TRUE(m_ZExtOrSelf(m_One()).match(One64Z)); |
| 518 |
EXPECT_FALSE(m_ZExtOrSelf(m_One()).match(One64S)); |
| 519 |
|
| 520 |
EXPECT_TRUE(m_SExtOrSelf(m_One()).match(One32)); |
| 521 |
EXPECT_FALSE(m_SExtOrSelf(m_One()).match(One64Z)); |
| 522 |
EXPECT_TRUE(m_SExtOrSelf(m_One()).match(One64S)); |
| 523 |
|
| 524 |
EXPECT_FALSE(m_ZExtOrSExt(m_One()).match(One32)); |
| 525 |
EXPECT_TRUE(m_ZExtOrSExt(m_One()).match(One64Z)); |
| 526 |
EXPECT_TRUE(m_ZExtOrSExt(m_One()).match(One64S)); |
| 527 |
|
| 528 |
EXPECT_TRUE(m_ZExtOrSExtOrSelf(m_One()).match(One32)); |
| 529 |
EXPECT_TRUE(m_ZExtOrSExtOrSelf(m_One()).match(One64Z)); |
| 530 |
EXPECT_TRUE(m_ZExtOrSExtOrSelf(m_One()).match(One64S)); |
| 531 |
} |
| 532 |
|
| 533 |
TEST_F(PatternMatchTest, Power2) { |
| 534 |
Value *C128 = IRB.getInt32(128); |
| 535 |
Value *CNeg128 = ConstantExpr::getNeg(cast<Constant>(C128)); |
| 536 |
|
| 537 |
EXPECT_TRUE(m_Power2().match(C128)); |
| 538 |
EXPECT_FALSE(m_Power2().match(CNeg128)); |
| 539 |
|
| 540 |
EXPECT_FALSE(m_NegatedPower2().match(C128)); |
| 541 |
EXPECT_TRUE(m_NegatedPower2().match(CNeg128)); |
| 542 |
|
| 543 |
Value *CIntMin = IRB.getInt64(APSInt::getSignedMinValue(64).getSExtValue()); |
| 544 |
Value *CNegIntMin = ConstantExpr::getNeg(cast<Constant>(CIntMin)); |
| 545 |
|
| 546 |
EXPECT_TRUE(m_Power2().match(CIntMin)); |
| 547 |
EXPECT_TRUE(m_Power2().match(CNegIntMin)); |
| 548 |
|
| 549 |
EXPECT_TRUE(m_NegatedPower2().match(CIntMin)); |
| 550 |
EXPECT_TRUE(m_NegatedPower2().match(CNegIntMin)); |
| 551 |
} |
| 552 |
|
| 553 |
TEST_F(PatternMatchTest, Not) { |
| 554 |
Value *C1 = IRB.getInt32(1); |
| 555 |
Value *C2 = IRB.getInt32(2); |
| 556 |
Value *C3 = IRB.getInt32(3); |
| 557 |
Instruction *Not = BinaryOperator::CreateXor(C1, C2); |
| 558 |
|
| 559 |
// When `m_Not` does not match the `not` itself, |
| 560 |
// it should not try to apply the inner matcher. |
| 561 |
Value *Val = C3; |
| 562 |
EXPECT_FALSE(m_Not(m_Value(Val)).match(Not)); |
| 563 |
EXPECT_EQ(Val, C3); |
| 564 |
Not->deleteValue(); |
| 565 |
} |
| 566 |
|
| 567 |
TEST_F(PatternMatchTest, CommutativeDeferredValue) { |
| 568 |
Value *X = IRB.getInt32(1); |
| 569 |
Value *Y = IRB.getInt32(2); |
| 570 |
|
| 571 |
{ |
| 572 |
Value *tX = X; |
| 573 |
EXPECT_TRUE(match(X, m_Deferred(tX))); |
| 574 |
EXPECT_FALSE(match(Y, m_Deferred(tX))); |
| 575 |
} |
| 576 |
{ |
| 577 |
const Value *tX = X; |
| 578 |
EXPECT_TRUE(match(X, m_Deferred(tX))); |
| 579 |
EXPECT_FALSE(match(Y, m_Deferred(tX))); |
| 580 |
} |
| 581 |
{ |
| 582 |
Value *const tX = X; |
| 583 |
EXPECT_TRUE(match(X, m_Deferred(tX))); |
| 584 |
EXPECT_FALSE(match(Y, m_Deferred(tX))); |
| 585 |
} |
| 586 |
{ |
| 587 |
const Value *const tX = X; |
| 588 |
EXPECT_TRUE(match(X, m_Deferred(tX))); |
| 589 |
EXPECT_FALSE(match(Y, m_Deferred(tX))); |
| 590 |
} |
| 591 |
|
| 592 |
{ |
| 593 |
Value *tX = nullptr; |
| 594 |
EXPECT_TRUE(match(IRB.CreateAnd(X, X), m_And(m_Value(tX), m_Deferred(tX)))); |
| 595 |
EXPECT_EQ(tX, X); |
| 596 |
} |
| 597 |
{ |
| 598 |
Value *tX = nullptr; |
| 599 |
EXPECT_FALSE( |
| 600 |
match(IRB.CreateAnd(X, Y), m_c_And(m_Value(tX), m_Deferred(tX)))); |
| 601 |
} |
| 602 |
|
| 603 |
auto checkMatch = [X, Y](Value *Pattern) { |
| 604 |
Value *tX = nullptr, *tY = nullptr; |
| 605 |
EXPECT_TRUE(match( |
| 606 |
Pattern, m_c_And(m_Value(tX), m_c_And(m_Deferred(tX), m_Value(tY))))); |
| 607 |
EXPECT_EQ(tX, X); |
| 608 |
EXPECT_EQ(tY, Y); |
| 609 |
}; |
| 610 |
|
| 611 |
checkMatch(IRB.CreateAnd(X, IRB.CreateAnd(X, Y))); |
| 612 |
checkMatch(IRB.CreateAnd(X, IRB.CreateAnd(Y, X))); |
| 613 |
checkMatch(IRB.CreateAnd(IRB.CreateAnd(X, Y), X)); |
| 614 |
checkMatch(IRB.CreateAnd(IRB.CreateAnd(Y, X), X)); |
| 615 |
} |
| 616 |
|
| 617 |
TEST_F(PatternMatchTest, FloatingPointOrderedMin) { |
| 618 |
Type *FltTy = IRB.getFloatTy(); |
| 619 |
Value *L = ConstantFP::get(FltTy, 1.0); |
| 620 |
Value *R = ConstantFP::get(FltTy, 2.0); |
| 621 |
Value *MatchL, *MatchR; |
| 622 |
|
| 623 |
// Test OLT. |
| 624 |
EXPECT_TRUE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR)) |
| 625 |
.match(IRB.CreateSelect(IRB.CreateFCmpOLT(L, R), L, R))); |
| 626 |
EXPECT_EQ(L, MatchL); |
| 627 |
EXPECT_EQ(R, MatchR); |
| 628 |
|
| 629 |
// Test OLE. |
| 630 |
EXPECT_TRUE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR)) |
| 631 |
.match(IRB.CreateSelect(IRB.CreateFCmpOLE(L, R), L, R))); |
| 632 |
EXPECT_EQ(L, MatchL); |
| 633 |
EXPECT_EQ(R, MatchR); |
| 634 |
|
| 635 |
// Test no match on OGE. |
| 636 |
EXPECT_FALSE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR)) |
| 637 |
.match(IRB.CreateSelect(IRB.CreateFCmpOGE(L, R), L, R))); |
| 638 |
|
| 639 |
// Test no match on OGT. |
| 640 |
EXPECT_FALSE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR)) |
| 641 |
.match(IRB.CreateSelect(IRB.CreateFCmpOGT(L, R), L, R))); |
| 642 |
|
| 643 |
// Test inverted selects. Note, that this "inverts" the ordering, e.g.: |
| 644 |
// %cmp = fcmp oge L, R |
| 645 |
// %min = select %cmp R, L |
| 646 |
// Given L == NaN |
| 647 |
// the above is expanded to %cmp == false ==> %min = L |
| 648 |
// which is true for UnordFMin, not OrdFMin, so test that: |
| 649 |
|
| 650 |
// [OU]GE with inverted select. |
| 651 |
EXPECT_FALSE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR)) |
| 652 |
.match(IRB.CreateSelect(IRB.CreateFCmpOGE(L, R), R, L))); |
| 653 |
EXPECT_TRUE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR)) |
| 654 |
.match(IRB.CreateSelect(IRB.CreateFCmpUGE(L, R), R, L))); |
| 655 |
EXPECT_EQ(L, MatchL); |
| 656 |
EXPECT_EQ(R, MatchR); |
| 657 |
|
| 658 |
// [OU]GT with inverted select. |
| 659 |
EXPECT_FALSE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR)) |
| 660 |
.match(IRB.CreateSelect(IRB.CreateFCmpOGT(L, R), R, L))); |
| 661 |
EXPECT_TRUE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR)) |
| 662 |
.match(IRB.CreateSelect(IRB.CreateFCmpUGT(L, R), R, L))); |
| 663 |
EXPECT_EQ(L, MatchL); |
| 664 |
EXPECT_EQ(R, MatchR); |
| 665 |
} |
| 666 |
|
| 667 |
TEST_F(PatternMatchTest, FloatingPointOrderedMax) { |
| 668 |
Type *FltTy = IRB.getFloatTy(); |
| 669 |
Value *L = ConstantFP::get(FltTy, 1.0); |
| 670 |
Value *R = ConstantFP::get(FltTy, 2.0); |
| 671 |
Value *MatchL, *MatchR; |
| 672 |
|
| 673 |
// Test OGT. |
| 674 |
EXPECT_TRUE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR)) |
| 675 |
.match(IRB.CreateSelect(IRB.CreateFCmpOGT(L, R), L, R))); |
| 676 |
EXPECT_EQ(L, MatchL); |
| 677 |
EXPECT_EQ(R, MatchR); |
| 678 |
|
| 679 |
// Test OGE. |
| 680 |
EXPECT_TRUE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR)) |
| 681 |
.match(IRB.CreateSelect(IRB.CreateFCmpOGE(L, R), L, R))); |
| 682 |
EXPECT_EQ(L, MatchL); |
| 683 |
EXPECT_EQ(R, MatchR); |
| 684 |
|
| 685 |
// Test no match on OLE. |
| 686 |
EXPECT_FALSE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR)) |
| 687 |
.match(IRB.CreateSelect(IRB.CreateFCmpOLE(L, R), L, R))); |
| 688 |
|
| 689 |
// Test no match on OLT. |
| 690 |
EXPECT_FALSE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR)) |
| 691 |
.match(IRB.CreateSelect(IRB.CreateFCmpOLT(L, R), L, R))); |
| 692 |
|
| 693 |
|
| 694 |
// Test inverted selects. Note, that this "inverts" the ordering, e.g.: |
| 695 |
// %cmp = fcmp ole L, R |
| 696 |
// %max = select %cmp, R, L |
| 697 |
// Given L == NaN, |
| 698 |
// the above is expanded to %cmp == false ==> %max == L |
| 699 |
// which is true for UnordFMax, not OrdFMax, so test that: |
| 700 |
|
| 701 |
// [OU]LE with inverted select. |
| 702 |
EXPECT_FALSE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR)) |
| 703 |
.match(IRB.CreateSelect(IRB.CreateFCmpOLE(L, R), R, L))); |
| 704 |
EXPECT_TRUE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR)) |
| 705 |
.match(IRB.CreateSelect(IRB.CreateFCmpULE(L, R), R, L))); |
| 706 |
EXPECT_EQ(L, MatchL); |
| 707 |
EXPECT_EQ(R, MatchR); |
| 708 |
|
| 709 |
// [OUT]LT with inverted select. |
| 710 |
EXPECT_FALSE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR)) |
| 711 |
.match(IRB.CreateSelect(IRB.CreateFCmpOLT(L, R), R, L))); |
| 712 |
EXPECT_TRUE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR)) |
| 713 |
.match(IRB.CreateSelect(IRB.CreateFCmpULT(L, R), R, L))); |
| 714 |
EXPECT_EQ(L, MatchL); |
| 715 |
EXPECT_EQ(R, MatchR); |
| 716 |
} |
| 717 |
|
| 718 |
TEST_F(PatternMatchTest, FloatingPointUnorderedMin) { |
| 719 |
Type *FltTy = IRB.getFloatTy(); |
| 720 |
Value *L = ConstantFP::get(FltTy, 1.0); |
| 721 |
Value *R = ConstantFP::get(FltTy, 2.0); |
| 722 |
Value *MatchL, *MatchR; |
| 723 |
|
| 724 |
// Test ULT. |
| 725 |
EXPECT_TRUE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR)) |
| 726 |
.match(IRB.CreateSelect(IRB.CreateFCmpULT(L, R), L, R))); |
| 727 |
EXPECT_EQ(L, MatchL); |
| 728 |
EXPECT_EQ(R, MatchR); |
| 729 |
|
| 730 |
// Test ULE. |
| 731 |
EXPECT_TRUE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR)) |
| 732 |
.match(IRB.CreateSelect(IRB.CreateFCmpULE(L, R), L, R))); |
| 733 |
EXPECT_EQ(L, MatchL); |
| 734 |
EXPECT_EQ(R, MatchR); |
| 735 |
|
| 736 |
// Test no match on UGE. |
| 737 |
EXPECT_FALSE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR)) |
| 738 |
.match(IRB.CreateSelect(IRB.CreateFCmpUGE(L, R), L, R))); |
| 739 |
|
| 740 |
// Test no match on UGT. |
| 741 |
EXPECT_FALSE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR)) |
| 742 |
.match(IRB.CreateSelect(IRB.CreateFCmpUGT(L, R), L, R))); |
| 743 |
|
| 744 |
// Test inverted selects. Note, that this "inverts" the ordering, e.g.: |
| 745 |
// %cmp = fcmp uge L, R |
| 746 |
// %min = select %cmp R, L |
| 747 |
// Given L == NaN |
| 748 |
// the above is expanded to %cmp == true ==> %min = R |
| 749 |
// which is true for OrdFMin, not UnordFMin, so test that: |
| 750 |
|
| 751 |
// [UO]GE with inverted select. |
| 752 |
EXPECT_FALSE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR)) |
| 753 |
.match(IRB.CreateSelect(IRB.CreateFCmpUGE(L, R), R, L))); |
| 754 |
EXPECT_TRUE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR)) |
| 755 |
.match(IRB.CreateSelect(IRB.CreateFCmpOGE(L, R), R, L))); |
| 756 |
EXPECT_EQ(L, MatchL); |
| 757 |
EXPECT_EQ(R, MatchR); |
| 758 |
|
| 759 |
// [UO]GT with inverted select. |
| 760 |
EXPECT_FALSE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR)) |
| 761 |
.match(IRB.CreateSelect(IRB.CreateFCmpUGT(L, R), R, L))); |
| 762 |
EXPECT_TRUE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR)) |
| 763 |
.match(IRB.CreateSelect(IRB.CreateFCmpOGT(L, R), R, L))); |
| 764 |
EXPECT_EQ(L, MatchL); |
| 765 |
EXPECT_EQ(R, MatchR); |
| 766 |
} |
| 767 |
|
| 768 |
TEST_F(PatternMatchTest, FloatingPointUnorderedMax) { |
| 769 |
Type *FltTy = IRB.getFloatTy(); |
| 770 |
Value *L = ConstantFP::get(FltTy, 1.0); |
| 771 |
Value *R = ConstantFP::get(FltTy, 2.0); |
| 772 |
Value *MatchL, *MatchR; |
| 773 |
|
| 774 |
// Test UGT. |
| 775 |
EXPECT_TRUE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR)) |
| 776 |
.match(IRB.CreateSelect(IRB.CreateFCmpUGT(L, R), L, R))); |
| 777 |
EXPECT_EQ(L, MatchL); |
| 778 |
EXPECT_EQ(R, MatchR); |
| 779 |
|
| 780 |
// Test UGE. |
| 781 |
EXPECT_TRUE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR)) |
| 782 |
.match(IRB.CreateSelect(IRB.CreateFCmpUGE(L, R), L, R))); |
| 783 |
EXPECT_EQ(L, MatchL); |
| 784 |
EXPECT_EQ(R, MatchR); |
| 785 |
|
| 786 |
// Test no match on ULE. |
| 787 |
EXPECT_FALSE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR)) |
| 788 |
.match(IRB.CreateSelect(IRB.CreateFCmpULE(L, R), L, R))); |
| 789 |
|
| 790 |
// Test no match on ULT. |
| 791 |
EXPECT_FALSE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR)) |
| 792 |
.match(IRB.CreateSelect(IRB.CreateFCmpULT(L, R), L, R))); |
| 793 |
|
| 794 |
// Test inverted selects. Note, that this "inverts" the ordering, e.g.: |
| 795 |
// %cmp = fcmp ule L, R |
| 796 |
// %max = select %cmp R, L |
| 797 |
// Given L == NaN |
| 798 |
// the above is expanded to %cmp == true ==> %max = R |
| 799 |
// which is true for OrdFMax, not UnordFMax, so test that: |
| 800 |
|
| 801 |
// [UO]LE with inverted select. |
| 802 |
EXPECT_FALSE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR)) |
| 803 |
.match(IRB.CreateSelect(IRB.CreateFCmpULE(L, R), R, L))); |
| 804 |
EXPECT_TRUE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR)) |
| 805 |
.match(IRB.CreateSelect(IRB.CreateFCmpOLE(L, R), R, L))); |
| 806 |
EXPECT_EQ(L, MatchL); |
| 807 |
EXPECT_EQ(R, MatchR); |
| 808 |
|
| 809 |
// [UO]LT with inverted select. |
| 810 |
EXPECT_FALSE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR)) |
| 811 |
.match(IRB.CreateSelect(IRB.CreateFCmpULT(L, R), R, L))); |
| 812 |
EXPECT_TRUE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR)) |
| 813 |
.match(IRB.CreateSelect(IRB.CreateFCmpOLT(L, R), R, L))); |
| 814 |
EXPECT_EQ(L, MatchL); |
| 815 |
EXPECT_EQ(R, MatchR); |
| 816 |
} |
| 817 |
|
| 818 |
TEST_F(PatternMatchTest, OverflowingBinOps) { |
| 819 |
Value *L = IRB.getInt32(1); |
| 820 |
Value *R = IRB.getInt32(2); |
| 821 |
Value *MatchL, *MatchR; |
| 822 |
|
| 823 |
EXPECT_TRUE( |
| 824 |
m_NSWAdd(m_Value(MatchL), m_Value(MatchR)).match(IRB.CreateNSWAdd(L, R))); |
| 825 |
EXPECT_EQ(L, MatchL); |
| 826 |
EXPECT_EQ(R, MatchR); |
| 827 |
MatchL = MatchR = nullptr; |
| 828 |
EXPECT_TRUE( |
| 829 |
m_NSWSub(m_Value(MatchL), m_Value(MatchR)).match(IRB.CreateNSWSub(L, R))); |
| 830 |
EXPECT_EQ(L, MatchL); |
| 831 |
EXPECT_EQ(R, MatchR); |
| 832 |
MatchL = MatchR = nullptr; |
| 833 |
EXPECT_TRUE( |
| 834 |
m_NSWMul(m_Value(MatchL), m_Value(MatchR)).match(IRB.CreateNSWMul(L, R))); |
| 835 |
EXPECT_EQ(L, MatchL); |
| 836 |
EXPECT_EQ(R, MatchR); |
| 837 |
MatchL = MatchR = nullptr; |
| 838 |
EXPECT_TRUE(m_NSWShl(m_Value(MatchL), m_Value(MatchR)).match( |
| 839 |
IRB.CreateShl(L, R, "", /* NUW */ false, /* NSW */ true))); |
| 840 |
EXPECT_EQ(L, MatchL); |
| 841 |
EXPECT_EQ(R, MatchR); |
| 842 |
|
| 843 |
EXPECT_TRUE( |
| 844 |
m_NUWAdd(m_Value(MatchL), m_Value(MatchR)).match(IRB.CreateNUWAdd(L, R))); |
| 845 |
EXPECT_EQ(L, MatchL); |
| 846 |
EXPECT_EQ(R, MatchR); |
| 847 |
MatchL = MatchR = nullptr; |
| 848 |
EXPECT_TRUE( |
| 849 |
m_NUWSub(m_Value(MatchL), m_Value(MatchR)).match(IRB.CreateNUWSub(L, R))); |
| 850 |
EXPECT_EQ(L, MatchL); |
| 851 |
EXPECT_EQ(R, MatchR); |
| 852 |
MatchL = MatchR = nullptr; |
| 853 |
EXPECT_TRUE( |
| 854 |
m_NUWMul(m_Value(MatchL), m_Value(MatchR)).match(IRB.CreateNUWMul(L, R))); |
| 855 |
EXPECT_EQ(L, MatchL); |
| 856 |
EXPECT_EQ(R, MatchR); |
| 857 |
MatchL = MatchR = nullptr; |
| 858 |
EXPECT_TRUE(m_NUWShl(m_Value(MatchL), m_Value(MatchR)).match( |
| 859 |
IRB.CreateShl(L, R, "", /* NUW */ true, /* NSW */ false))); |
| 860 |
EXPECT_EQ(L, MatchL); |
| 861 |
EXPECT_EQ(R, MatchR); |
| 862 |
|
| 863 |
EXPECT_FALSE(m_NSWAdd(m_Value(), m_Value()).match(IRB.CreateAdd(L, R))); |
| 864 |
EXPECT_FALSE(m_NSWAdd(m_Value(), m_Value()).match(IRB.CreateNUWAdd(L, R))); |
| 865 |
EXPECT_FALSE(m_NSWAdd(m_Value(), m_Value()).match(IRB.CreateNSWSub(L, R))); |
| 866 |
EXPECT_FALSE(m_NSWSub(m_Value(), m_Value()).match(IRB.CreateSub(L, R))); |
| 867 |
EXPECT_FALSE(m_NSWSub(m_Value(), m_Value()).match(IRB.CreateNUWSub(L, R))); |
| 868 |
EXPECT_FALSE(m_NSWSub(m_Value(), m_Value()).match(IRB.CreateNSWAdd(L, R))); |
| 869 |
EXPECT_FALSE(m_NSWMul(m_Value(), m_Value()).match(IRB.CreateMul(L, R))); |
| 870 |
EXPECT_FALSE(m_NSWMul(m_Value(), m_Value()).match(IRB.CreateNUWMul(L, R))); |
| 871 |
EXPECT_FALSE(m_NSWMul(m_Value(), m_Value()).match(IRB.CreateNSWAdd(L, R))); |
| 872 |
EXPECT_FALSE(m_NSWShl(m_Value(), m_Value()).match(IRB.CreateShl(L, R))); |
| 873 |
EXPECT_FALSE(m_NSWShl(m_Value(), m_Value()).match( |
| 874 |
IRB.CreateShl(L, R, "", /* NUW */ true, /* NSW */ false))); |
| 875 |
EXPECT_FALSE(m_NSWShl(m_Value(), m_Value()).match(IRB.CreateNSWAdd(L, R))); |
| 876 |
|
| 877 |
EXPECT_FALSE(m_NUWAdd(m_Value(), m_Value()).match(IRB.CreateAdd(L, R))); |
| 878 |
EXPECT_FALSE(m_NUWAdd(m_Value(), m_Value()).match(IRB.CreateNSWAdd(L, R))); |
| 879 |
EXPECT_FALSE(m_NUWAdd(m_Value(), m_Value()).match(IRB.CreateNUWSub(L, R))); |
| 880 |
EXPECT_FALSE(m_NUWSub(m_Value(), m_Value()).match(IRB.CreateSub(L, R))); |
| 881 |
EXPECT_FALSE(m_NUWSub(m_Value(), m_Value()).match(IRB.CreateNSWSub(L, R))); |
| 882 |
EXPECT_FALSE(m_NUWSub(m_Value(), m_Value()).match(IRB.CreateNUWAdd(L, R))); |
| 883 |
EXPECT_FALSE(m_NUWMul(m_Value(), m_Value()).match(IRB.CreateMul(L, R))); |
| 884 |
EXPECT_FALSE(m_NUWMul(m_Value(), m_Value()).match(IRB.CreateNSWMul(L, R))); |
| 885 |
EXPECT_FALSE(m_NUWMul(m_Value(), m_Value()).match(IRB.CreateNUWAdd(L, R))); |
| 886 |
EXPECT_FALSE(m_NUWShl(m_Value(), m_Value()).match(IRB.CreateShl(L, R))); |
| 887 |
EXPECT_FALSE(m_NUWShl(m_Value(), m_Value()).match( |
| 888 |
IRB.CreateShl(L, R, "", /* NUW */ false, /* NSW */ true))); |
| 889 |
EXPECT_FALSE(m_NUWShl(m_Value(), m_Value()).match(IRB.CreateNUWAdd(L, R))); |
| 890 |
} |
| 891 |
|
| 892 |
TEST_F(PatternMatchTest, LoadStoreOps) { |
| 893 |
// Create this load/store sequence: |
| 894 |
// |
| 895 |
// %p = alloca i32* |
| 896 |
// %0 = load i32*, i32** %p |
| 897 |
// store i32 42, i32* %0 |
| 898 |
|
| 899 |
Value *Alloca = IRB.CreateAlloca(IRB.getInt32Ty()); |
| 900 |
Value *LoadInst = IRB.CreateLoad(IRB.getInt32Ty(), Alloca); |
| 901 |
Value *FourtyTwo = IRB.getInt32(42); |
| 902 |
Value *StoreInst = IRB.CreateStore(FourtyTwo, Alloca); |
| 903 |
Value *MatchLoad, *MatchStoreVal, *MatchStorePointer; |
| 904 |
|
| 905 |
EXPECT_TRUE(m_Load(m_Value(MatchLoad)).match(LoadInst)); |
| 906 |
EXPECT_EQ(Alloca, MatchLoad); |
| 907 |
|
| 908 |
EXPECT_TRUE(m_Load(m_Specific(Alloca)).match(LoadInst)); |
| 909 |
|
| 910 |
EXPECT_FALSE(m_Load(m_Value(MatchLoad)).match(Alloca)); |
| 911 |
|
| 912 |
EXPECT_TRUE(m_Store(m_Value(MatchStoreVal), m_Value(MatchStorePointer)) |
| 913 |
.match(StoreInst)); |
| 914 |
EXPECT_EQ(FourtyTwo, MatchStoreVal); |
| 915 |
EXPECT_EQ(Alloca, MatchStorePointer); |
| 916 |
|
| 917 |
EXPECT_FALSE(m_Store(m_Value(MatchStoreVal), m_Value(MatchStorePointer)) |
| 918 |
.match(Alloca)); |
| 919 |
|
| 920 |
EXPECT_TRUE(m_Store(m_SpecificInt(42), m_Specific(Alloca)) |
| 921 |
.match(StoreInst)); |
| 922 |
EXPECT_FALSE(m_Store(m_SpecificInt(42), m_Specific(FourtyTwo)) |
| 923 |
.match(StoreInst)); |
| 924 |
EXPECT_FALSE(m_Store(m_SpecificInt(43), m_Specific(Alloca)) |
| 925 |
.match(StoreInst)); |
| 926 |
} |
| 927 |
|
| 928 |
TEST_F(PatternMatchTest, VectorOps) { |
| 929 |
// Build up small tree of vector operations |
| 930 |
// |
| 931 |
// Val = 0 + 1 |
| 932 |
// Val2 = Val + 3 |
| 933 |
// VI1 = insertelement <2 x i8> undef, i8 1, i32 0 = <1, undef> |
| 934 |
// VI2 = insertelement <2 x i8> %VI1, i8 %Val2, i8 %Val = <1, 4> |
| 935 |
// VI3 = insertelement <2 x i8> %VI1, i8 %Val2, i32 1 = <1, 4> |
| 936 |
// VI4 = insertelement <2 x i8> %VI1, i8 2, i8 %Val = <1, 2> |
| 937 |
// |
| 938 |
// SI1 = shufflevector <2 x i8> %VI1, <2 x i8> undef, zeroinitializer |
| 939 |
// SI2 = shufflevector <2 x i8> %VI3, <2 x i8> %VI4, <2 x i8> <i8 0, i8 2> |
| 940 |
// SI3 = shufflevector <2 x i8> %VI3, <2 x i8> undef, zeroinitializer |
| 941 |
// SI4 = shufflevector <2 x i8> %VI4, <2 x i8> undef, zeroinitializer |
| 942 |
// |
| 943 |
// SP1 = VectorSplat(2, i8 2) |
| 944 |
// SP2 = VectorSplat(2, i8 %Val) |
| 945 |
Type *VecTy = FixedVectorType::get(IRB.getInt8Ty(), 2); |
| 946 |
Type *i32 = IRB.getInt32Ty(); |
| 947 |
Type *i32VecTy = FixedVectorType::get(i32, 2); |
| 948 |
|
| 949 |
Value *Val = IRB.CreateAdd(IRB.getInt8(0), IRB.getInt8(1)); |
| 950 |
Value *Val2 = IRB.CreateAdd(Val, IRB.getInt8(3)); |
| 951 |
|
| 952 |
SmallVector<Constant *, 2> VecElemIdxs; |
| 953 |
VecElemIdxs.push_back(ConstantInt::get(i32, 0)); |
| 954 |
VecElemIdxs.push_back(ConstantInt::get(i32, 2)); |
| 955 |
auto *IdxVec = ConstantVector::get(VecElemIdxs); |
| 956 |
|
| 957 |
Value *VI1 = IRB.CreateInsertElement(VecTy, IRB.getInt8(1), (uint64_t)0); |
| 958 |
Value *VI2 = IRB.CreateInsertElement(VI1, Val2, Val); |
| 959 |
Value *VI3 = IRB.CreateInsertElement(VI1, Val2, (uint64_t)1); |
| 960 |
Value *VI4 = IRB.CreateInsertElement(VI1, IRB.getInt8(2), Val); |
| 961 |
|
| 962 |
Value *EX1 = IRB.CreateExtractElement(VI4, Val); |
| 963 |
Value *EX2 = IRB.CreateExtractElement(VI4, (uint64_t)0); |
| 964 |
Value *EX3 = IRB.CreateExtractElement(IdxVec, (uint64_t)1); |
| 965 |
|
| 966 |
Constant *Zero = ConstantAggregateZero::get(i32VecTy); |
| 967 |
SmallVector<int, 16> ZeroMask; |
| 968 |
ShuffleVectorInst::getShuffleMask(Zero, ZeroMask); |
| 969 |
|
| 970 |
Value *SI1 = IRB.CreateShuffleVector(VI1, ZeroMask); |
| 971 |
Value *SI2 = IRB.CreateShuffleVector(VI3, VI4, IdxVec); |
| 972 |
Value *SI3 = IRB.CreateShuffleVector(VI3, ZeroMask); |
| 973 |
Value *SI4 = IRB.CreateShuffleVector(VI4, ZeroMask); |
| 974 |
|
| 975 |
Value *SP1 = IRB.CreateVectorSplat(2, IRB.getInt8(2)); |
| 976 |
Value *SP2 = IRB.CreateVectorSplat(2, Val); |
| 977 |
|
| 978 |
Value *A = nullptr, *B = nullptr, *C = nullptr; |
| 979 |
|
| 980 |
// Test matching insertelement |
| 981 |
EXPECT_TRUE(match(VI1, m_InsertElt(m_Value(), m_Value(), m_Value()))); |
| 982 |
EXPECT_TRUE( |
| 983 |
match(VI1, m_InsertElt(m_Undef(), m_ConstantInt(), m_ConstantInt()))); |
| 984 |
EXPECT_TRUE( |
| 985 |
match(VI1, m_InsertElt(m_Undef(), m_ConstantInt(), m_Zero()))); |
| 986 |
EXPECT_TRUE( |
| 987 |
match(VI1, m_InsertElt(m_Undef(), m_SpecificInt(1), m_Zero()))); |
| 988 |
EXPECT_TRUE(match(VI2, m_InsertElt(m_Value(), m_Value(), m_Value()))); |
| 989 |
EXPECT_FALSE( |
| 990 |
match(VI2, m_InsertElt(m_Value(), m_Value(), m_ConstantInt()))); |
| 991 |
EXPECT_FALSE( |
| 992 |
match(VI2, m_InsertElt(m_Value(), m_ConstantInt(), m_Value()))); |
| 993 |
EXPECT_FALSE(match(VI2, m_InsertElt(m_Constant(), m_Value(), m_Value()))); |
| 994 |
EXPECT_TRUE(match(VI3, m_InsertElt(m_Value(A), m_Value(B), m_Value(C)))); |
| 995 |
EXPECT_TRUE(A == VI1); |
| 996 |
EXPECT_TRUE(B == Val2); |
| 997 |
EXPECT_TRUE(isa<ConstantInt>(C)); |
| 998 |
A = B = C = nullptr; // reset |
| 999 |
|
| 1000 |
// Test matching extractelement |
| 1001 |
EXPECT_TRUE(match(EX1, m_ExtractElt(m_Value(A), m_Value(B)))); |
| 1002 |
EXPECT_TRUE(A == VI4); |
| 1003 |
EXPECT_TRUE(B == Val); |
| 1004 |
A = B = C = nullptr; // reset |
| 1005 |
EXPECT_FALSE(match(EX1, m_ExtractElt(m_Value(), m_ConstantInt()))); |
| 1006 |
EXPECT_TRUE(match(EX2, m_ExtractElt(m_Value(), m_ConstantInt()))); |
| 1007 |
EXPECT_TRUE(match(EX3, m_ExtractElt(m_Constant(), m_ConstantInt()))); |
| 1008 |
|
| 1009 |
// Test matching shufflevector |
| 1010 |
ArrayRef<int> Mask; |
| 1011 |
EXPECT_TRUE(match(SI1, m_Shuffle(m_Value(), m_Undef(), m_ZeroMask()))); |
| 1012 |
EXPECT_TRUE(match(SI2, m_Shuffle(m_Value(A), m_Value(B), m_Mask(Mask)))); |
| 1013 |
EXPECT_TRUE(A == VI3); |
| 1014 |
EXPECT_TRUE(B == VI4); |
| 1015 |
A = B = C = nullptr; // reset |
| 1016 |
|
| 1017 |
// Test matching the vector splat pattern |
| 1018 |
EXPECT_TRUE(match( |
| 1019 |
SI1, |
| 1020 |
m_Shuffle(m_InsertElt(m_Undef(), m_SpecificInt(1), m_Zero()), |
| 1021 |
m_Undef(), m_ZeroMask()))); |
| 1022 |
EXPECT_FALSE(match( |
| 1023 |
SI3, m_Shuffle(m_InsertElt(m_Undef(), m_Value(), m_Zero()), |
| 1024 |
m_Undef(), m_ZeroMask()))); |
| 1025 |
EXPECT_FALSE(match( |
| 1026 |
SI4, m_Shuffle(m_InsertElt(m_Undef(), m_Value(), m_Zero()), |
| 1027 |
m_Undef(), m_ZeroMask()))); |
| 1028 |
EXPECT_TRUE(match( |
| 1029 |
SP1, |
| 1030 |
m_Shuffle(m_InsertElt(m_Undef(), m_SpecificInt(2), m_Zero()), |
| 1031 |
m_Undef(), m_ZeroMask()))); |
| 1032 |
EXPECT_TRUE(match( |
| 1033 |
SP2, m_Shuffle(m_InsertElt(m_Undef(), m_Value(A), m_Zero()), |
| 1034 |
m_Undef(), m_ZeroMask()))); |
| 1035 |
EXPECT_TRUE(A == Val); |
| 1036 |
} |
| 1037 |
|
| 1038 |
TEST_F(PatternMatchTest, UndefPoisonMix) { |
| 1039 |
Type *ScalarTy = IRB.getInt8Ty(); |
| 1040 |
ArrayType *ArrTy = ArrayType::get(ScalarTy, 2); |
| 1041 |
StructType *StTy = StructType::get(ScalarTy, ScalarTy); |
| 1042 |
StructType *StTy2 = StructType::get(ScalarTy, StTy); |
| 1043 |
StructType *StTy3 = StructType::get(StTy, ScalarTy); |
| 1044 |
Constant *Zero = ConstantInt::getNullValue(ScalarTy); |
| 1045 |
UndefValue *U = UndefValue::get(ScalarTy); |
| 1046 |
UndefValue *P = PoisonValue::get(ScalarTy); |
| 1047 |
|
| 1048 |
EXPECT_TRUE(match(ConstantVector::get({U, P}), m_Undef())); |
| 1049 |
EXPECT_TRUE(match(ConstantVector::get({P, U}), m_Undef())); |
| 1050 |
|
| 1051 |
EXPECT_TRUE(match(ConstantArray::get(ArrTy, {U, P}), m_Undef())); |
| 1052 |
EXPECT_TRUE(match(ConstantArray::get(ArrTy, {P, U}), m_Undef())); |
| 1053 |
|
| 1054 |
auto *UP = ConstantStruct::get(StTy, {U, P}); |
| 1055 |
EXPECT_TRUE(match(ConstantStruct::get(StTy2, {U, UP}), m_Undef())); |
| 1056 |
EXPECT_TRUE(match(ConstantStruct::get(StTy2, {P, UP}), m_Undef())); |
| 1057 |
EXPECT_TRUE(match(ConstantStruct::get(StTy3, {UP, U}), m_Undef())); |
| 1058 |
EXPECT_TRUE(match(ConstantStruct::get(StTy3, {UP, P}), m_Undef())); |
| 1059 |
|
| 1060 |
EXPECT_FALSE(match(ConstantStruct::get(StTy, {U, Zero}), m_Undef())); |
| 1061 |
EXPECT_FALSE(match(ConstantStruct::get(StTy, {Zero, U}), m_Undef())); |
| 1062 |
EXPECT_FALSE(match(ConstantStruct::get(StTy, {P, Zero}), m_Undef())); |
| 1063 |
EXPECT_FALSE(match(ConstantStruct::get(StTy, {Zero, P}), m_Undef())); |
| 1064 |
|
| 1065 |
EXPECT_FALSE(match(ConstantStruct::get(StTy2, {Zero, UP}), m_Undef())); |
| 1066 |
EXPECT_FALSE(match(ConstantStruct::get(StTy3, {UP, Zero}), m_Undef())); |
| 1067 |
} |
| 1068 |
|
| 1069 |
TEST_F(PatternMatchTest, VectorUndefInt) { |
| 1070 |
Type *ScalarTy = IRB.getInt8Ty(); |
| 1071 |
Type *VectorTy = FixedVectorType::get(ScalarTy, 4); |
| 1072 |
Constant *ScalarUndef = UndefValue::get(ScalarTy); |
| 1073 |
Constant *VectorUndef = UndefValue::get(VectorTy); |
| 1074 |
Constant *ScalarZero = Constant::getNullValue(ScalarTy); |
| 1075 |
Constant *VectorZero = Constant::getNullValue(VectorTy); |
| 1076 |
|
| 1077 |
SmallVector<Constant *, 4> Elems; |
| 1078 |
Elems.push_back(ScalarUndef); |
| 1079 |
Elems.push_back(ScalarZero); |
| 1080 |
Elems.push_back(ScalarUndef); |
| 1081 |
Elems.push_back(ScalarZero); |
| 1082 |
Constant *VectorZeroUndef = ConstantVector::get(Elems); |
| 1083 |
|
| 1084 |
EXPECT_TRUE(match(ScalarUndef, m_Undef())); |
| 1085 |
EXPECT_TRUE(match(VectorUndef, m_Undef())); |
| 1086 |
EXPECT_FALSE(match(ScalarZero, m_Undef())); |
| 1087 |
EXPECT_FALSE(match(VectorZero, m_Undef())); |
| 1088 |
EXPECT_FALSE(match(VectorZeroUndef, m_Undef())); |
| 1089 |
|
| 1090 |
EXPECT_FALSE(match(ScalarUndef, m_Zero())); |
| 1091 |
EXPECT_FALSE(match(VectorUndef, m_Zero())); |
| 1092 |
EXPECT_TRUE(match(ScalarZero, m_Zero())); |
| 1093 |
EXPECT_TRUE(match(VectorZero, m_Zero())); |
| 1094 |
EXPECT_TRUE(match(VectorZeroUndef, m_Zero())); |
| 1095 |
|
| 1096 |
const APInt *C; |
| 1097 |
// Regardless of whether undefs are allowed, |
| 1098 |
// a fully undef constant does not match. |
| 1099 |
EXPECT_FALSE(match(ScalarUndef, m_APInt(C))); |
| 1100 |
EXPECT_FALSE(match(ScalarUndef, m_APIntForbidUndef(C))); |
| 1101 |
EXPECT_FALSE(match(ScalarUndef, m_APIntAllowUndef(C))); |
| 1102 |
EXPECT_FALSE(match(VectorUndef, m_APInt(C))); |
| 1103 |
EXPECT_FALSE(match(VectorUndef, m_APIntForbidUndef(C))); |
| 1104 |
EXPECT_FALSE(match(VectorUndef, m_APIntAllowUndef(C))); |
| 1105 |
|
| 1106 |
// We can always match simple constants and simple splats. |
| 1107 |
C = nullptr; |
| 1108 |
EXPECT_TRUE(match(ScalarZero, m_APInt(C))); |
| 1109 |
EXPECT_TRUE(C->isZero()); |
| 1110 |
C = nullptr; |
| 1111 |
EXPECT_TRUE(match(ScalarZero, m_APIntForbidUndef(C))); |
| 1112 |
EXPECT_TRUE(C->isZero()); |
| 1113 |
C = nullptr; |
| 1114 |
EXPECT_TRUE(match(ScalarZero, m_APIntAllowUndef(C))); |
| 1115 |
EXPECT_TRUE(C->isZero()); |
| 1116 |
C = nullptr; |
| 1117 |
EXPECT_TRUE(match(VectorZero, m_APInt(C))); |
| 1118 |
EXPECT_TRUE(C->isZero()); |
| 1119 |
C = nullptr; |
| 1120 |
EXPECT_TRUE(match(VectorZero, m_APIntForbidUndef(C))); |
| 1121 |
EXPECT_TRUE(C->isZero()); |
| 1122 |
C = nullptr; |
| 1123 |
EXPECT_TRUE(match(VectorZero, m_APIntAllowUndef(C))); |
| 1124 |
EXPECT_TRUE(C->isZero()); |
| 1125 |
|
| 1126 |
// Whether splats with undef can be matched depends on the matcher. |
| 1127 |
EXPECT_FALSE(match(VectorZeroUndef, m_APInt(C))); |
| 1128 |
EXPECT_FALSE(match(VectorZeroUndef, m_APIntForbidUndef(C))); |
| 1129 |
C = nullptr; |
| 1130 |
EXPECT_TRUE(match(VectorZeroUndef, m_APIntAllowUndef(C))); |
| 1131 |
EXPECT_TRUE(C->isZero()); |
| 1132 |
} |
| 1133 |
|
| 1134 |
TEST_F(PatternMatchTest, VectorUndefFloat) { |
| 1135 |
Type *ScalarTy = IRB.getFloatTy(); |
| 1136 |
Type *VectorTy = FixedVectorType::get(ScalarTy, 4); |
| 1137 |
Constant *ScalarUndef = UndefValue::get(ScalarTy); |
| 1138 |
Constant *VectorUndef = UndefValue::get(VectorTy); |
| 1139 |
Constant *ScalarZero = Constant::getNullValue(ScalarTy); |
| 1140 |
Constant *VectorZero = Constant::getNullValue(VectorTy); |
| 1141 |
Constant *ScalarPosInf = ConstantFP::getInfinity(ScalarTy, false); |
| 1142 |
Constant *ScalarNegInf = ConstantFP::getInfinity(ScalarTy, true); |
| 1143 |
Constant *ScalarNaN = ConstantFP::getNaN(ScalarTy, true); |
| 1144 |
|
| 1145 |
Constant *VectorZeroUndef = |
| 1146 |
ConstantVector::get({ScalarUndef, ScalarZero, ScalarUndef, ScalarZero}); |
| 1147 |
|
| 1148 |
Constant *VectorInfUndef = ConstantVector::get( |
| 1149 |
{ScalarPosInf, ScalarNegInf, ScalarUndef, ScalarPosInf}); |
| 1150 |
|
| 1151 |
Constant *VectorNaNUndef = |
| 1152 |
ConstantVector::get({ScalarUndef, ScalarNaN, ScalarNaN, ScalarNaN}); |
| 1153 |
|
| 1154 |
EXPECT_TRUE(match(ScalarUndef, m_Undef())); |
| 1155 |
EXPECT_TRUE(match(VectorUndef, m_Undef())); |
| 1156 |
EXPECT_FALSE(match(ScalarZero, m_Undef())); |
| 1157 |
EXPECT_FALSE(match(VectorZero, m_Undef())); |
| 1158 |
EXPECT_FALSE(match(VectorZeroUndef, m_Undef())); |
| 1159 |
EXPECT_FALSE(match(VectorInfUndef, m_Undef())); |
| 1160 |
EXPECT_FALSE(match(VectorNaNUndef, m_Undef())); |
| 1161 |
|
| 1162 |
EXPECT_FALSE(match(ScalarUndef, m_AnyZeroFP())); |
| 1163 |
EXPECT_FALSE(match(VectorUndef, m_AnyZeroFP())); |
| 1164 |
EXPECT_TRUE(match(ScalarZero, m_AnyZeroFP())); |
| 1165 |
EXPECT_TRUE(match(VectorZero, m_AnyZeroFP())); |
| 1166 |
EXPECT_TRUE(match(VectorZeroUndef, m_AnyZeroFP())); |
| 1167 |
EXPECT_FALSE(match(VectorInfUndef, m_AnyZeroFP())); |
| 1168 |
EXPECT_FALSE(match(VectorNaNUndef, m_AnyZeroFP())); |
| 1169 |
|
| 1170 |
EXPECT_FALSE(match(ScalarUndef, m_NaN())); |
| 1171 |
EXPECT_FALSE(match(VectorUndef, m_NaN())); |
| 1172 |
EXPECT_FALSE(match(VectorZeroUndef, m_NaN())); |
| 1173 |
EXPECT_FALSE(match(ScalarPosInf, m_NaN())); |
| 1174 |
EXPECT_FALSE(match(ScalarNegInf, m_NaN())); |
| 1175 |
EXPECT_TRUE(match(ScalarNaN, m_NaN())); |
| 1176 |
EXPECT_FALSE(match(VectorInfUndef, m_NaN())); |
| 1177 |
EXPECT_TRUE(match(VectorNaNUndef, m_NaN())); |
| 1178 |
|
| 1179 |
EXPECT_FALSE(match(ScalarUndef, m_NonNaN())); |
| 1180 |
EXPECT_FALSE(match(VectorUndef, m_NonNaN())); |
| 1181 |
EXPECT_TRUE(match(VectorZeroUndef, m_NonNaN())); |
| 1182 |
EXPECT_TRUE(match(ScalarPosInf, m_NonNaN())); |
| 1183 |
EXPECT_TRUE(match(ScalarNegInf, m_NonNaN())); |
| 1184 |
EXPECT_FALSE(match(ScalarNaN, m_NonNaN())); |
| 1185 |
EXPECT_TRUE(match(VectorInfUndef, m_NonNaN())); |
| 1186 |
EXPECT_FALSE(match(VectorNaNUndef, m_NonNaN())); |
| 1187 |
|
| 1188 |
EXPECT_FALSE(match(ScalarUndef, m_Inf())); |
| 1189 |
EXPECT_FALSE(match(VectorUndef, m_Inf())); |
| 1190 |
EXPECT_FALSE(match(VectorZeroUndef, m_Inf())); |
| 1191 |
EXPECT_TRUE(match(ScalarPosInf, m_Inf())); |
| 1192 |
EXPECT_TRUE(match(ScalarNegInf, m_Inf())); |
| 1193 |
EXPECT_FALSE(match(ScalarNaN, m_Inf())); |
| 1194 |
EXPECT_TRUE(match(VectorInfUndef, m_Inf())); |
| 1195 |
EXPECT_FALSE(match(VectorNaNUndef, m_Inf())); |
| 1196 |
|
| 1197 |
EXPECT_FALSE(match(ScalarUndef, m_NonInf())); |
| 1198 |
EXPECT_FALSE(match(VectorUndef, m_NonInf())); |
| 1199 |
EXPECT_TRUE(match(VectorZeroUndef, m_NonInf())); |
| 1200 |
EXPECT_FALSE(match(ScalarPosInf, m_NonInf())); |
| 1201 |
EXPECT_FALSE(match(ScalarNegInf, m_NonInf())); |
| 1202 |
EXPECT_TRUE(match(ScalarNaN, m_NonInf())); |
| 1203 |
EXPECT_FALSE(match(VectorInfUndef, m_NonInf())); |
| 1204 |
EXPECT_TRUE(match(VectorNaNUndef, m_NonInf())); |
| 1205 |
|
| 1206 |
EXPECT_FALSE(match(ScalarUndef, m_Finite())); |
| 1207 |
EXPECT_FALSE(match(VectorUndef, m_Finite())); |
| 1208 |
EXPECT_TRUE(match(VectorZeroUndef, m_Finite())); |
| 1209 |
EXPECT_FALSE(match(ScalarPosInf, m_Finite())); |
| 1210 |
EXPECT_FALSE(match(ScalarNegInf, m_Finite())); |
| 1211 |
EXPECT_FALSE(match(ScalarNaN, m_Finite())); |
| 1212 |
EXPECT_FALSE(match(VectorInfUndef, m_Finite())); |
| 1213 |
EXPECT_FALSE(match(VectorNaNUndef, m_Finite())); |
| 1214 |
|
| 1215 |
const APFloat *C; |
| 1216 |
// Regardless of whether undefs are allowed, |
| 1217 |
// a fully undef constant does not match. |
| 1218 |
EXPECT_FALSE(match(ScalarUndef, m_APFloat(C))); |
| 1219 |
EXPECT_FALSE(match(ScalarUndef, m_APFloatForbidUndef(C))); |
| 1220 |
EXPECT_FALSE(match(ScalarUndef, m_APFloatAllowUndef(C))); |
| 1221 |
EXPECT_FALSE(match(VectorUndef, m_APFloat(C))); |
| 1222 |
EXPECT_FALSE(match(VectorUndef, m_APFloatForbidUndef(C))); |
| 1223 |
EXPECT_FALSE(match(VectorUndef, m_APFloatAllowUndef(C))); |
| 1224 |
|
| 1225 |
// We can always match simple constants and simple splats. |
| 1226 |
C = nullptr; |
| 1227 |
EXPECT_TRUE(match(ScalarZero, m_APFloat(C))); |
| 1228 |
EXPECT_TRUE(C->isZero()); |
| 1229 |
C = nullptr; |
| 1230 |
EXPECT_TRUE(match(ScalarZero, m_APFloatForbidUndef(C))); |
| 1231 |
EXPECT_TRUE(C->isZero()); |
| 1232 |
C = nullptr; |
| 1233 |
EXPECT_TRUE(match(ScalarZero, m_APFloatAllowUndef(C))); |
| 1234 |
EXPECT_TRUE(C->isZero()); |
| 1235 |
C = nullptr; |
| 1236 |
EXPECT_TRUE(match(VectorZero, m_APFloat(C))); |
| 1237 |
EXPECT_TRUE(C->isZero()); |
| 1238 |
C = nullptr; |
| 1239 |
EXPECT_TRUE(match(VectorZero, m_APFloatForbidUndef(C))); |
| 1240 |
EXPECT_TRUE(C->isZero()); |
| 1241 |
C = nullptr; |
| 1242 |
EXPECT_TRUE(match(VectorZero, m_APFloatAllowUndef(C))); |
| 1243 |
EXPECT_TRUE(C->isZero()); |
| 1244 |
|
| 1245 |
// Whether splats with undef can be matched depends on the matcher. |
| 1246 |
EXPECT_FALSE(match(VectorZeroUndef, m_APFloat(C))); |
| 1247 |
EXPECT_FALSE(match(VectorZeroUndef, m_APFloatForbidUndef(C))); |
| 1248 |
C = nullptr; |
| 1249 |
EXPECT_TRUE(match(VectorZeroUndef, m_APFloatAllowUndef(C))); |
| 1250 |
EXPECT_TRUE(C->isZero()); |
| 1251 |
C = nullptr; |
| 1252 |
EXPECT_TRUE(match(VectorZeroUndef, m_Finite(C))); |
| 1253 |
EXPECT_TRUE(C->isZero()); |
| 1254 |
} |
| 1255 |
|
| 1256 |
TEST_F(PatternMatchTest, FloatingPointFNeg) { |
| 1257 |
Type *FltTy = IRB.getFloatTy(); |
| 1258 |
Value *One = ConstantFP::get(FltTy, 1.0); |
| 1259 |
Value *Z = ConstantFP::get(FltTy, 0.0); |
| 1260 |
Value *NZ = ConstantFP::get(FltTy, -0.0); |
| 1261 |
Value *V = IRB.CreateFNeg(One); |
| 1262 |
Value *V1 = IRB.CreateFSub(NZ, One); |
| 1263 |
Value *V2 = IRB.CreateFSub(Z, One); |
| 1264 |
Value *V3 = IRB.CreateFAdd(NZ, One); |
| 1265 |
Value *Match; |
| 1266 |
|
| 1267 |
// Test FNeg(1.0) |
| 1268 |
EXPECT_TRUE(match(V, m_FNeg(m_Value(Match)))); |
| 1269 |
EXPECT_EQ(One, Match); |
| 1270 |
|
| 1271 |
// Test FSub(-0.0, 1.0) |
| 1272 |
EXPECT_TRUE(match(V1, m_FNeg(m_Value(Match)))); |
| 1273 |
EXPECT_EQ(One, Match); |
| 1274 |
|
| 1275 |
// Test FSub(0.0, 1.0) |
| 1276 |
EXPECT_FALSE(match(V2, m_FNeg(m_Value(Match)))); |
| 1277 |
cast<Instruction>(V2)->setHasNoSignedZeros(true); |
| 1278 |
EXPECT_TRUE(match(V2, m_FNeg(m_Value(Match)))); |
| 1279 |
EXPECT_EQ(One, Match); |
| 1280 |
|
| 1281 |
// Test FAdd(-0.0, 1.0) |
| 1282 |
EXPECT_FALSE(match(V3, m_FNeg(m_Value(Match)))); |
| 1283 |
} |
| 1284 |
|
| 1285 |
TEST_F(PatternMatchTest, CondBranchTest) { |
| 1286 |
BasicBlock *TrueBB = BasicBlock::Create(Ctx, "TrueBB", F); |
| 1287 |
BasicBlock *FalseBB = BasicBlock::Create(Ctx, "FalseBB", F); |
| 1288 |
Value *Br1 = IRB.CreateCondBr(IRB.getTrue(), TrueBB, FalseBB); |
| 1289 |
|
| 1290 |
EXPECT_TRUE(match(Br1, m_Br(m_Value(), m_BasicBlock(), m_BasicBlock()))); |
| 1291 |
|
| 1292 |
BasicBlock *A, *B; |
| 1293 |
EXPECT_TRUE(match(Br1, m_Br(m_Value(), m_BasicBlock(A), m_BasicBlock(B)))); |
| 1294 |
EXPECT_EQ(TrueBB, A); |
| 1295 |
EXPECT_EQ(FalseBB, B); |
| 1296 |
|
| 1297 |
EXPECT_FALSE( |
| 1298 |
match(Br1, m_Br(m_Value(), m_SpecificBB(FalseBB), m_BasicBlock()))); |
| 1299 |
EXPECT_FALSE( |
| 1300 |
match(Br1, m_Br(m_Value(), m_BasicBlock(), m_SpecificBB(TrueBB)))); |
| 1301 |
EXPECT_FALSE( |
| 1302 |
match(Br1, m_Br(m_Value(), m_SpecificBB(FalseBB), m_BasicBlock(TrueBB)))); |
| 1303 |
EXPECT_TRUE( |
| 1304 |
match(Br1, m_Br(m_Value(), m_SpecificBB(TrueBB), m_BasicBlock(FalseBB)))); |
| 1305 |
|
| 1306 |
// Check we can use m_Deferred with branches. |
| 1307 |
EXPECT_FALSE(match(Br1, m_Br(m_Value(), m_BasicBlock(A), m_Deferred(A)))); |
| 1308 |
Value *Br2 = IRB.CreateCondBr(IRB.getTrue(), TrueBB, TrueBB); |
| 1309 |
A = nullptr; |
| 1310 |
EXPECT_TRUE(match(Br2, m_Br(m_Value(), m_BasicBlock(A), m_Deferred(A)))); |
| 1311 |
} |
| 1312 |
|
| 1313 |
TEST_F(PatternMatchTest, WithOverflowInst) { |
| 1314 |
Value *Add = IRB.CreateBinaryIntrinsic(Intrinsic::uadd_with_overflow, |
| 1315 |
IRB.getInt32(0), IRB.getInt32(0)); |
| 1316 |
Value *Add0 = IRB.CreateExtractValue(Add, 0); |
| 1317 |
Value *Add1 = IRB.CreateExtractValue(Add, 1); |
| 1318 |
|
| 1319 |
EXPECT_TRUE(match(Add0, m_ExtractValue<0>(m_Value()))); |
| 1320 |
EXPECT_FALSE(match(Add0, m_ExtractValue<1>(m_Value()))); |
| 1321 |
EXPECT_FALSE(match(Add1, m_ExtractValue<0>(m_Value()))); |
| 1322 |
EXPECT_TRUE(match(Add1, m_ExtractValue<1>(m_Value()))); |
| 1323 |
EXPECT_FALSE(match(Add, m_ExtractValue<1>(m_Value()))); |
| 1324 |
EXPECT_FALSE(match(Add, m_ExtractValue<1>(m_Value()))); |
| 1325 |
|
| 1326 |
WithOverflowInst *WOI; |
| 1327 |
EXPECT_FALSE(match(Add0, m_WithOverflowInst(WOI))); |
| 1328 |
EXPECT_FALSE(match(Add1, m_WithOverflowInst(WOI))); |
| 1329 |
EXPECT_TRUE(match(Add, m_WithOverflowInst(WOI))); |
| 1330 |
|
| 1331 |
EXPECT_TRUE(match(Add0, m_ExtractValue<0>(m_WithOverflowInst(WOI)))); |
| 1332 |
EXPECT_EQ(Add, WOI); |
| 1333 |
EXPECT_TRUE(match(Add1, m_ExtractValue<1>(m_WithOverflowInst(WOI)))); |
| 1334 |
EXPECT_EQ(Add, WOI); |
| 1335 |
} |
| 1336 |
|
| 1337 |
TEST_F(PatternMatchTest, MinMaxIntrinsics) { |
| 1338 |
Type *Ty = IRB.getInt32Ty(); |
| 1339 |
Value *L = ConstantInt::get(Ty, 1); |
| 1340 |
Value *R = ConstantInt::get(Ty, 2); |
| 1341 |
Value *MatchL, *MatchR; |
| 1342 |
|
| 1343 |
// Check for intrinsic ID match and capture of operands. |
| 1344 |
EXPECT_TRUE(m_SMax(m_Value(MatchL), m_Value(MatchR)) |
| 1345 |
.match(IRB.CreateBinaryIntrinsic(Intrinsic::smax, L, R))); |
| 1346 |
EXPECT_EQ(L, MatchL); |
| 1347 |
EXPECT_EQ(R, MatchR); |
| 1348 |
|
| 1349 |
EXPECT_TRUE(m_SMin(m_Value(MatchL), m_Value(MatchR)) |
| 1350 |
.match(IRB.CreateBinaryIntrinsic(Intrinsic::smin, L, R))); |
| 1351 |
EXPECT_EQ(L, MatchL); |
| 1352 |
EXPECT_EQ(R, MatchR); |
| 1353 |
|
| 1354 |
EXPECT_TRUE(m_UMax(m_Value(MatchL), m_Value(MatchR)) |
| 1355 |
.match(IRB.CreateBinaryIntrinsic(Intrinsic::umax, L, R))); |
| 1356 |
EXPECT_EQ(L, MatchL); |
| 1357 |
EXPECT_EQ(R, MatchR); |
| 1358 |
|
| 1359 |
EXPECT_TRUE(m_UMin(m_Value(MatchL), m_Value(MatchR)) |
| 1360 |
.match(IRB.CreateBinaryIntrinsic(Intrinsic::umin, L, R))); |
| 1361 |
EXPECT_EQ(L, MatchL); |
| 1362 |
EXPECT_EQ(R, MatchR); |
| 1363 |
|
| 1364 |
// Check for intrinsic ID mismatch. |
| 1365 |
EXPECT_FALSE(m_SMax(m_Value(MatchL), m_Value(MatchR)) |
| 1366 |
.match(IRB.CreateBinaryIntrinsic(Intrinsic::smin, L, R))); |
| 1367 |
EXPECT_FALSE(m_SMin(m_Value(MatchL), m_Value(MatchR)) |
| 1368 |
.match(IRB.CreateBinaryIntrinsic(Intrinsic::umax, L, R))); |
| 1369 |
EXPECT_FALSE(m_UMax(m_Value(MatchL), m_Value(MatchR)) |
| 1370 |
.match(IRB.CreateBinaryIntrinsic(Intrinsic::umin, L, R))); |
| 1371 |
EXPECT_FALSE(m_UMin(m_Value(MatchL), m_Value(MatchR)) |
| 1372 |
.match(IRB.CreateBinaryIntrinsic(Intrinsic::smax, L, R))); |
| 1373 |
} |
| 1374 |
|
| 1375 |
TEST_F(PatternMatchTest, IntrinsicMatcher) { |
| 1376 |
Value *Name = IRB.CreateAlloca(IRB.getInt8Ty()); |
| 1377 |
Value *Hash = IRB.getInt64(0); |
| 1378 |
Value *Num = IRB.getInt32(1); |
| 1379 |
Value *Index = IRB.getInt32(2); |
| 1380 |
Value *Step = IRB.getInt64(3); |
| 1381 |
|
| 1382 |
Value *Ops[] = {Name, Hash, Num, Index, Step}; |
| 1383 |
Module *M = BB->getParent()->getParent(); |
| 1384 |
Function *TheFn = |
| 1385 |
Intrinsic::getDeclaration(M, Intrinsic::instrprof_increment_step); |
| 1386 |
|
| 1387 |
Value *Intrinsic5 = CallInst::Create(TheFn, Ops, "", BB); |
| 1388 |
|
| 1389 |
// Match without capturing. |
| 1390 |
EXPECT_TRUE(match( |
| 1391 |
Intrinsic5, m_Intrinsic<Intrinsic::instrprof_increment_step>( |
| 1392 |
m_Value(), m_Value(), m_Value(), m_Value(), m_Value()))); |
| 1393 |
EXPECT_FALSE(match( |
| 1394 |
Intrinsic5, m_Intrinsic<Intrinsic::memmove>( |
| 1395 |
m_Value(), m_Value(), m_Value(), m_Value(), m_Value()))); |
| 1396 |
|
| 1397 |
// Match with capturing. |
| 1398 |
Value *Arg1 = nullptr; |
| 1399 |
Value *Arg2 = nullptr; |
| 1400 |
Value *Arg3 = nullptr; |
| 1401 |
Value *Arg4 = nullptr; |
| 1402 |
Value *Arg5 = nullptr; |
| 1403 |
EXPECT_TRUE( |
| 1404 |
match(Intrinsic5, m_Intrinsic<Intrinsic::instrprof_increment_step>( |
| 1405 |
m_Value(Arg1), m_Value(Arg2), m_Value(Arg3), |
| 1406 |
m_Value(Arg4), m_Value(Arg5)))); |
| 1407 |
EXPECT_EQ(Arg1, Name); |
| 1408 |
EXPECT_EQ(Arg2, Hash); |
| 1409 |
EXPECT_EQ(Arg3, Num); |
| 1410 |
EXPECT_EQ(Arg4, Index); |
| 1411 |
EXPECT_EQ(Arg5, Step); |
| 1412 |
|
| 1413 |
// Match specific second argument. |
| 1414 |
EXPECT_TRUE( |
| 1415 |
match(Intrinsic5, |
| 1416 |
m_Intrinsic<Intrinsic::instrprof_increment_step>( |
| 1417 |
m_Value(), m_SpecificInt(0), m_Value(), m_Value(), m_Value()))); |
| 1418 |
EXPECT_FALSE( |
| 1419 |
match(Intrinsic5, m_Intrinsic<Intrinsic::instrprof_increment_step>( |
| 1420 |
m_Value(), m_SpecificInt(10), m_Value(), m_Value(), |
| 1421 |
m_Value()))); |
| 1422 |
|
| 1423 |
// Match specific third argument. |
| 1424 |
EXPECT_TRUE( |
| 1425 |
match(Intrinsic5, |
| 1426 |
m_Intrinsic<Intrinsic::instrprof_increment_step>( |
| 1427 |
m_Value(), m_Value(), m_SpecificInt(1), m_Value(), m_Value()))); |
| 1428 |
EXPECT_FALSE( |
| 1429 |
match(Intrinsic5, m_Intrinsic<Intrinsic::instrprof_increment_step>( |
| 1430 |
m_Value(), m_Value(), m_SpecificInt(10), m_Value(), |
| 1431 |
m_Value()))); |
| 1432 |
|
| 1433 |
// Match specific fourth argument. |
| 1434 |
EXPECT_TRUE( |
| 1435 |
match(Intrinsic5, |
| 1436 |
m_Intrinsic<Intrinsic::instrprof_increment_step>( |
| 1437 |
m_Value(), m_Value(), m_Value(), m_SpecificInt(2), m_Value()))); |
| 1438 |
EXPECT_FALSE( |
| 1439 |
match(Intrinsic5, m_Intrinsic<Intrinsic::instrprof_increment_step>( |
| 1440 |
m_Value(), m_Value(), m_Value(), m_SpecificInt(10), |
| 1441 |
m_Value()))); |
| 1442 |
|
| 1443 |
// Match specific fifth argument. |
| 1444 |
EXPECT_TRUE( |
| 1445 |
match(Intrinsic5, |
| 1446 |
m_Intrinsic<Intrinsic::instrprof_increment_step>( |
| 1447 |
m_Value(), m_Value(), m_Value(), m_Value(), m_SpecificInt(3)))); |
| 1448 |
EXPECT_FALSE( |
| 1449 |
match(Intrinsic5, m_Intrinsic<Intrinsic::instrprof_increment_step>( |
| 1450 |
m_Value(), m_Value(), m_Value(), m_Value(), |
| 1451 |
m_SpecificInt(10)))); |
| 1452 |
} |
| 1453 |
|
| 1454 |
namespace { |
| 1455 |
|
| 1456 |
struct is_unsigned_zero_pred { |
| 1457 |
bool isValue(const APInt &C) { return C.isZero(); } |
| 1458 |
}; |
| 1459 |
|
| 1460 |
struct is_float_zero_pred { |
| 1461 |
bool isValue(const APFloat &C) { return C.isZero(); } |
| 1462 |
}; |
| 1463 |
|
| 1464 |
template <typename T> struct always_true_pred { |
| 1465 |
bool isValue(const T &) { return true; } |
| 1466 |
}; |
| 1467 |
|
| 1468 |
template <typename T> struct always_false_pred { |
| 1469 |
bool isValue(const T &) { return false; } |
| 1470 |
}; |
| 1471 |
|
| 1472 |
struct is_unsigned_max_pred { |
| 1473 |
bool isValue(const APInt &C) { return C.isMaxValue(); } |
| 1474 |
}; |
| 1475 |
|
| 1476 |
struct is_float_nan_pred { |
| 1477 |
bool isValue(const APFloat &C) { return C.isNaN(); } |
| 1478 |
}; |
| 1479 |
|
| 1480 |
} // namespace |
| 1481 |
|
| 1482 |
TEST_F(PatternMatchTest, ConstantPredicateType) { |
| 1483 |
|
| 1484 |
// Scalar integer |
| 1485 |
APInt U32Max = APInt::getAllOnes(32); |
| 1486 |
APInt U32Zero = APInt::getZero(32); |
| 1487 |
APInt U32DeadBeef(32, 0xDEADBEEF); |
| 1488 |
|
| 1489 |
Type *U32Ty = Type::getInt32Ty(Ctx); |
| 1490 |
|
| 1491 |
Constant *CU32Max = Constant::getIntegerValue(U32Ty, U32Max); |
| 1492 |
Constant *CU32Zero = Constant::getIntegerValue(U32Ty, U32Zero); |
| 1493 |
Constant *CU32DeadBeef = Constant::getIntegerValue(U32Ty, U32DeadBeef); |
| 1494 |
|
| 1495 |
EXPECT_TRUE(match(CU32Max, cst_pred_ty<is_unsigned_max_pred>())); |
| 1496 |
EXPECT_FALSE(match(CU32Max, cst_pred_ty<is_unsigned_zero_pred>())); |
| 1497 |
EXPECT_TRUE(match(CU32Max, cst_pred_ty<always_true_pred<APInt>>())); |
| 1498 |
EXPECT_FALSE(match(CU32Max, cst_pred_ty<always_false_pred<APInt>>())); |
| 1499 |
|
| 1500 |
EXPECT_FALSE(match(CU32Zero, cst_pred_ty<is_unsigned_max_pred>())); |
| 1501 |
EXPECT_TRUE(match(CU32Zero, cst_pred_ty<is_unsigned_zero_pred>())); |
| 1502 |
EXPECT_TRUE(match(CU32Zero, cst_pred_ty<always_true_pred<APInt>>())); |
| 1503 |
EXPECT_FALSE(match(CU32Zero, cst_pred_ty<always_false_pred<APInt>>())); |
| 1504 |
|
| 1505 |
EXPECT_FALSE(match(CU32DeadBeef, cst_pred_ty<is_unsigned_max_pred>())); |
| 1506 |
EXPECT_FALSE(match(CU32DeadBeef, cst_pred_ty<is_unsigned_zero_pred>())); |
| 1507 |
EXPECT_TRUE(match(CU32DeadBeef, cst_pred_ty<always_true_pred<APInt>>())); |
| 1508 |
EXPECT_FALSE(match(CU32DeadBeef, cst_pred_ty<always_false_pred<APInt>>())); |
| 1509 |
|
| 1510 |
// Scalar float |
| 1511 |
APFloat F32NaN = APFloat::getNaN(APFloat::IEEEsingle()); |
| 1512 |
APFloat F32Zero = APFloat::getZero(APFloat::IEEEsingle()); |
| 1513 |
APFloat F32Pi(3.14f); |
| 1514 |
|
| 1515 |
Type *F32Ty = Type::getFloatTy(Ctx); |
| 1516 |
|
| 1517 |
Constant *CF32NaN = ConstantFP::get(F32Ty, F32NaN); |
| 1518 |
Constant *CF32Zero = ConstantFP::get(F32Ty, F32Zero); |
| 1519 |
Constant *CF32Pi = ConstantFP::get(F32Ty, F32Pi); |
| 1520 |
|
| 1521 |
EXPECT_TRUE(match(CF32NaN, cstfp_pred_ty<is_float_nan_pred>())); |
| 1522 |
EXPECT_FALSE(match(CF32NaN, cstfp_pred_ty<is_float_zero_pred>())); |
| 1523 |
EXPECT_TRUE(match(CF32NaN, cstfp_pred_ty<always_true_pred<APFloat>>())); |
| 1524 |
EXPECT_FALSE(match(CF32NaN, cstfp_pred_ty<always_false_pred<APFloat>>())); |
| 1525 |
|
| 1526 |
EXPECT_FALSE(match(CF32Zero, cstfp_pred_ty<is_float_nan_pred>())); |
| 1527 |
EXPECT_TRUE(match(CF32Zero, cstfp_pred_ty<is_float_zero_pred>())); |
| 1528 |
EXPECT_TRUE(match(CF32Zero, cstfp_pred_ty<always_true_pred<APFloat>>())); |
| 1529 |
EXPECT_FALSE(match(CF32Zero, cstfp_pred_ty<always_false_pred<APFloat>>())); |
| 1530 |
|
| 1531 |
EXPECT_FALSE(match(CF32Pi, cstfp_pred_ty<is_float_nan_pred>())); |
| 1532 |
EXPECT_FALSE(match(CF32Pi, cstfp_pred_ty<is_float_zero_pred>())); |
| 1533 |
EXPECT_TRUE(match(CF32Pi, cstfp_pred_ty<always_true_pred<APFloat>>())); |
| 1534 |
EXPECT_FALSE(match(CF32Pi, cstfp_pred_ty<always_false_pred<APFloat>>())); |
| 1535 |
|
| 1536 |
auto FixedEC = ElementCount::getFixed(4); |
| 1537 |
auto ScalableEC = ElementCount::getScalable(4); |
| 1538 |
|
| 1539 |
// Vector splat |
| 1540 |
|
| 1541 |
for (auto EC : {FixedEC, ScalableEC}) { |
| 1542 |
// integer |
| 1543 |
|
| 1544 |
Constant *CSplatU32Max = ConstantVector::getSplat(EC, CU32Max); |
| 1545 |
Constant *CSplatU32Zero = ConstantVector::getSplat(EC, CU32Zero); |
| 1546 |
Constant *CSplatU32DeadBeef = ConstantVector::getSplat(EC, CU32DeadBeef); |
| 1547 |
|
| 1548 |
EXPECT_TRUE(match(CSplatU32Max, cst_pred_ty<is_unsigned_max_pred>())); |
| 1549 |
EXPECT_FALSE(match(CSplatU32Max, cst_pred_ty<is_unsigned_zero_pred>())); |
| 1550 |
EXPECT_TRUE(match(CSplatU32Max, cst_pred_ty<always_true_pred<APInt>>())); |
| 1551 |
EXPECT_FALSE(match(CSplatU32Max, cst_pred_ty<always_false_pred<APInt>>())); |
| 1552 |
|
| 1553 |
EXPECT_FALSE(match(CSplatU32Zero, cst_pred_ty<is_unsigned_max_pred>())); |
| 1554 |
EXPECT_TRUE(match(CSplatU32Zero, cst_pred_ty<is_unsigned_zero_pred>())); |
| 1555 |
EXPECT_TRUE(match(CSplatU32Zero, cst_pred_ty<always_true_pred<APInt>>())); |
| 1556 |
EXPECT_FALSE(match(CSplatU32Zero, cst_pred_ty<always_false_pred<APInt>>())); |
| 1557 |
|
| 1558 |
EXPECT_FALSE(match(CSplatU32DeadBeef, cst_pred_ty<is_unsigned_max_pred>())); |
| 1559 |
EXPECT_FALSE( |
| 1560 |
match(CSplatU32DeadBeef, cst_pred_ty<is_unsigned_zero_pred>())); |
| 1561 |
EXPECT_TRUE( |
| 1562 |
match(CSplatU32DeadBeef, cst_pred_ty<always_true_pred<APInt>>())); |
| 1563 |
EXPECT_FALSE( |
| 1564 |
match(CSplatU32DeadBeef, cst_pred_ty<always_false_pred<APInt>>())); |
| 1565 |
|
| 1566 |
// float |
| 1567 |
|
| 1568 |
Constant *CSplatF32NaN = ConstantVector::getSplat(EC, CF32NaN); |
| 1569 |
Constant *CSplatF32Zero = ConstantVector::getSplat(EC, CF32Zero); |
| 1570 |
Constant *CSplatF32Pi = ConstantVector::getSplat(EC, CF32Pi); |
| 1571 |
|
| 1572 |
EXPECT_TRUE(match(CSplatF32NaN, cstfp_pred_ty<is_float_nan_pred>())); |
| 1573 |
EXPECT_FALSE(match(CSplatF32NaN, cstfp_pred_ty<is_float_zero_pred>())); |
| 1574 |
EXPECT_TRUE( |
| 1575 |
match(CSplatF32NaN, cstfp_pred_ty<always_true_pred<APFloat>>())); |
| 1576 |
EXPECT_FALSE( |
| 1577 |
match(CSplatF32NaN, cstfp_pred_ty<always_false_pred<APFloat>>())); |
| 1578 |
|
| 1579 |
EXPECT_FALSE(match(CSplatF32Zero, cstfp_pred_ty<is_float_nan_pred>())); |
| 1580 |
EXPECT_TRUE(match(CSplatF32Zero, cstfp_pred_ty<is_float_zero_pred>())); |
| 1581 |
EXPECT_TRUE( |
| 1582 |
match(CSplatF32Zero, cstfp_pred_ty<always_true_pred<APFloat>>())); |
| 1583 |
EXPECT_FALSE( |
| 1584 |
match(CSplatF32Zero, cstfp_pred_ty<always_false_pred<APFloat>>())); |
| 1585 |
|
| 1586 |
EXPECT_FALSE(match(CSplatF32Pi, cstfp_pred_ty<is_float_nan_pred>())); |
| 1587 |
EXPECT_FALSE(match(CSplatF32Pi, cstfp_pred_ty<is_float_zero_pred>())); |
| 1588 |
EXPECT_TRUE(match(CSplatF32Pi, cstfp_pred_ty<always_true_pred<APFloat>>())); |
| 1589 |
EXPECT_FALSE( |
| 1590 |
match(CSplatF32Pi, cstfp_pred_ty<always_false_pred<APFloat>>())); |
| 1591 |
} |
| 1592 |
|
| 1593 |
// Int arbitrary vector |
| 1594 |
|
| 1595 |
Constant *CMixedU32 = ConstantVector::get({CU32Max, CU32Zero, CU32DeadBeef}); |
| 1596 |
Constant *CU32Undef = UndefValue::get(U32Ty); |
| 1597 |
Constant *CU32MaxWithUndef = |
| 1598 |
ConstantVector::get({CU32Undef, CU32Max, CU32Undef}); |
| 1599 |
|
| 1600 |
EXPECT_FALSE(match(CMixedU32, cst_pred_ty<is_unsigned_max_pred>())); |
| 1601 |
EXPECT_FALSE(match(CMixedU32, cst_pred_ty<is_unsigned_zero_pred>())); |
| 1602 |
EXPECT_TRUE(match(CMixedU32, cst_pred_ty<always_true_pred<APInt>>())); |
| 1603 |
EXPECT_FALSE(match(CMixedU32, cst_pred_ty<always_false_pred<APInt>>())); |
| 1604 |
|
| 1605 |
EXPECT_TRUE(match(CU32MaxWithUndef, cst_pred_ty<is_unsigned_max_pred>())); |
| 1606 |
EXPECT_FALSE(match(CU32MaxWithUndef, cst_pred_ty<is_unsigned_zero_pred>())); |
| 1607 |
EXPECT_TRUE(match(CU32MaxWithUndef, cst_pred_ty<always_true_pred<APInt>>())); |
| 1608 |
EXPECT_FALSE( |
| 1609 |
match(CU32MaxWithUndef, cst_pred_ty<always_false_pred<APInt>>())); |
| 1610 |
|
| 1611 |
// Float arbitrary vector |
| 1612 |
|
| 1613 |
Constant *CMixedF32 = ConstantVector::get({CF32NaN, CF32Zero, CF32Pi}); |
| 1614 |
Constant *CF32Undef = UndefValue::get(F32Ty); |
| 1615 |
Constant *CF32NaNWithUndef = |
| 1616 |
ConstantVector::get({CF32Undef, CF32NaN, CF32Undef}); |
| 1617 |
|
| 1618 |
EXPECT_FALSE(match(CMixedF32, cstfp_pred_ty<is_float_nan_pred>())); |
| 1619 |
EXPECT_FALSE(match(CMixedF32, cstfp_pred_ty<is_float_zero_pred>())); |
| 1620 |
EXPECT_TRUE(match(CMixedF32, cstfp_pred_ty<always_true_pred<APFloat>>())); |
| 1621 |
EXPECT_FALSE(match(CMixedF32, cstfp_pred_ty<always_false_pred<APFloat>>())); |
| 1622 |
|
| 1623 |
EXPECT_TRUE(match(CF32NaNWithUndef, cstfp_pred_ty<is_float_nan_pred>())); |
| 1624 |
EXPECT_FALSE(match(CF32NaNWithUndef, cstfp_pred_ty<is_float_zero_pred>())); |
| 1625 |
EXPECT_TRUE( |
| 1626 |
match(CF32NaNWithUndef, cstfp_pred_ty<always_true_pred<APFloat>>())); |
| 1627 |
EXPECT_FALSE( |
| 1628 |
match(CF32NaNWithUndef, cstfp_pred_ty<always_false_pred<APFloat>>())); |
| 1629 |
} |
| 1630 |
|
| 1631 |
TEST_F(PatternMatchTest, InsertValue) { |
| 1632 |
Type *StructTy = StructType::create(IRB.getContext(), |
| 1633 |
{IRB.getInt32Ty(), IRB.getInt64Ty()}); |
| 1634 |
Value *Ins0 = |
| 1635 |
IRB.CreateInsertValue(UndefValue::get(StructTy), IRB.getInt32(20), 0); |
| 1636 |
Value *Ins1 = IRB.CreateInsertValue(Ins0, IRB.getInt64(90), 1); |
| 1637 |
|
| 1638 |
EXPECT_TRUE(match(Ins0, m_InsertValue<0>(m_Value(), m_Value()))); |
| 1639 |
EXPECT_FALSE(match(Ins0, m_InsertValue<1>(m_Value(), m_Value()))); |
| 1640 |
EXPECT_FALSE(match(Ins1, m_InsertValue<0>(m_Value(), m_Value()))); |
| 1641 |
EXPECT_TRUE(match(Ins1, m_InsertValue<1>(m_Value(), m_Value()))); |
| 1642 |
|
| 1643 |
EXPECT_TRUE(match(Ins0, m_InsertValue<0>(m_Undef(), m_SpecificInt(20)))); |
| 1644 |
EXPECT_FALSE(match(Ins0, m_InsertValue<0>(m_Undef(), m_SpecificInt(0)))); |
| 1645 |
|
| 1646 |
EXPECT_TRUE( |
| 1647 |
match(Ins1, m_InsertValue<1>(m_InsertValue<0>(m_Value(), m_Value()), |
| 1648 |
m_SpecificInt(90)))); |
| 1649 |
EXPECT_FALSE(match(IRB.getInt64(99), m_InsertValue<0>(m_Value(), m_Value()))); |
| 1650 |
} |
| 1651 |
|
| 1652 |
TEST_F(PatternMatchTest, LogicalSelects) { |
| 1653 |
Value *Alloca = IRB.CreateAlloca(IRB.getInt1Ty()); |
| 1654 |
Value *X = IRB.CreateLoad(IRB.getInt1Ty(), Alloca); |
| 1655 |
Value *Y = IRB.CreateLoad(IRB.getInt1Ty(), Alloca); |
| 1656 |
Constant *T = IRB.getInt1(true); |
| 1657 |
Constant *F = IRB.getInt1(false); |
| 1658 |
Value *And = IRB.CreateSelect(X, Y, F); |
| 1659 |
Value *Or = IRB.CreateSelect(X, T, Y); |
| 1660 |
|
| 1661 |
// Logical and: |
| 1662 |
// Check basic no-capture logic - opcode and constant must match. |
| 1663 |
EXPECT_TRUE(match(And, m_LogicalAnd(m_Value(), m_Value()))); |
| 1664 |
EXPECT_TRUE(match(And, m_c_LogicalAnd(m_Value(), m_Value()))); |
| 1665 |
EXPECT_FALSE(match(And, m_LogicalOr(m_Value(), m_Value()))); |
| 1666 |
EXPECT_FALSE(match(And, m_c_LogicalOr(m_Value(), m_Value()))); |
| 1667 |
|
| 1668 |
// Check with captures. |
| 1669 |
EXPECT_TRUE(match(And, m_LogicalAnd(m_Specific(X), m_Value()))); |
| 1670 |
EXPECT_TRUE(match(And, m_LogicalAnd(m_Value(), m_Specific(Y)))); |
| 1671 |
EXPECT_TRUE(match(And, m_LogicalAnd(m_Specific(X), m_Specific(Y)))); |
| 1672 |
|
| 1673 |
EXPECT_FALSE(match(And, m_LogicalAnd(m_Specific(Y), m_Value()))); |
| 1674 |
EXPECT_FALSE(match(And, m_LogicalAnd(m_Value(), m_Specific(X)))); |
| 1675 |
EXPECT_FALSE(match(And, m_LogicalAnd(m_Specific(Y), m_Specific(X)))); |
| 1676 |
|
| 1677 |
EXPECT_FALSE(match(And, m_LogicalAnd(m_Specific(X), m_Specific(X)))); |
| 1678 |
EXPECT_FALSE(match(And, m_LogicalAnd(m_Specific(Y), m_Specific(Y)))); |
| 1679 |
|
| 1680 |
// Check captures for commutative match. |
| 1681 |
EXPECT_TRUE(match(And, m_c_LogicalAnd(m_Specific(X), m_Value()))); |
| 1682 |
EXPECT_TRUE(match(And, m_c_LogicalAnd(m_Value(), m_Specific(Y)))); |
| 1683 |
EXPECT_TRUE(match(And, m_c_LogicalAnd(m_Specific(X), m_Specific(Y)))); |
| 1684 |
|
| 1685 |
EXPECT_TRUE(match(And, m_c_LogicalAnd(m_Specific(Y), m_Value()))); |
| 1686 |
EXPECT_TRUE(match(And, m_c_LogicalAnd(m_Value(), m_Specific(X)))); |
| 1687 |
EXPECT_TRUE(match(And, m_c_LogicalAnd(m_Specific(Y), m_Specific(X)))); |
| 1688 |
|
| 1689 |
EXPECT_FALSE(match(And, m_c_LogicalAnd(m_Specific(X), m_Specific(X)))); |
| 1690 |
EXPECT_FALSE(match(And, m_c_LogicalAnd(m_Specific(Y), m_Specific(Y)))); |
| 1691 |
|
| 1692 |
// Logical or: |
| 1693 |
// Check basic no-capture logic - opcode and constant must match. |
| 1694 |
EXPECT_TRUE(match(Or, m_LogicalOr(m_Value(), m_Value()))); |
| 1695 |
EXPECT_TRUE(match(Or, m_c_LogicalOr(m_Value(), m_Value()))); |
| 1696 |
EXPECT_FALSE(match(Or, m_LogicalAnd(m_Value(), m_Value()))); |
| 1697 |
EXPECT_FALSE(match(Or, m_c_LogicalAnd(m_Value(), m_Value()))); |
| 1698 |
|
| 1699 |
// Check with captures. |
| 1700 |
EXPECT_TRUE(match(Or, m_LogicalOr(m_Specific(X), m_Value()))); |
| 1701 |
EXPECT_TRUE(match(Or, m_LogicalOr(m_Value(), m_Specific(Y)))); |
| 1702 |
EXPECT_TRUE(match(Or, m_LogicalOr(m_Specific(X), m_Specific(Y)))); |
| 1703 |
|
| 1704 |
EXPECT_FALSE(match(Or, m_LogicalOr(m_Specific(Y), m_Value()))); |
| 1705 |
EXPECT_FALSE(match(Or, m_LogicalOr(m_Value(), m_Specific(X)))); |
| 1706 |
EXPECT_FALSE(match(Or, m_LogicalOr(m_Specific(Y), m_Specific(X)))); |
| 1707 |
|
| 1708 |
EXPECT_FALSE(match(Or, m_LogicalOr(m_Specific(X), m_Specific(X)))); |
| 1709 |
EXPECT_FALSE(match(Or, m_LogicalOr(m_Specific(Y), m_Specific(Y)))); |
| 1710 |
|
| 1711 |
// Check captures for commutative match. |
| 1712 |
EXPECT_TRUE(match(Or, m_c_LogicalOr(m_Specific(X), m_Value()))); |
| 1713 |
EXPECT_TRUE(match(Or, m_c_LogicalOr(m_Value(), m_Specific(Y)))); |
| 1714 |
EXPECT_TRUE(match(Or, m_c_LogicalOr(m_Specific(X), m_Specific(Y)))); |
| 1715 |
|
| 1716 |
EXPECT_TRUE(match(Or, m_c_LogicalOr(m_Specific(Y), m_Value()))); |
| 1717 |
EXPECT_TRUE(match(Or, m_c_LogicalOr(m_Value(), m_Specific(X)))); |
| 1718 |
EXPECT_TRUE(match(Or, m_c_LogicalOr(m_Specific(Y), m_Specific(X)))); |
| 1719 |
|
| 1720 |
EXPECT_FALSE(match(Or, m_c_LogicalOr(m_Specific(X), m_Specific(X)))); |
| 1721 |
EXPECT_FALSE(match(Or, m_c_LogicalOr(m_Specific(Y), m_Specific(Y)))); |
| 1722 |
} |
| 1723 |
|
| 1724 |
TEST_F(PatternMatchTest, VectorLogicalSelects) { |
| 1725 |
Type *i1 = IRB.getInt1Ty(); |
| 1726 |
Type *v3i1 = FixedVectorType::get(i1, 3); |
| 1727 |
|
| 1728 |
Value *Alloca = IRB.CreateAlloca(i1); |
| 1729 |
Value *AllocaVec = IRB.CreateAlloca(v3i1); |
| 1730 |
Value *Scalar = IRB.CreateLoad(i1, Alloca); |
| 1731 |
Value *Vector = IRB.CreateLoad(v3i1, AllocaVec); |
| 1732 |
Constant *F = Constant::getNullValue(v3i1); |
| 1733 |
Constant *T = Constant::getAllOnesValue(v3i1); |
| 1734 |
|
| 1735 |
// select <3 x i1> Vector, <3 x i1> Vector, <3 x i1> <i1 0, i1 0, i1 0> |
| 1736 |
Value *VecAnd = IRB.CreateSelect(Vector, Vector, F); |
| 1737 |
|
| 1738 |
// select i1 Scalar, <3 x i1> Vector, <3 x i1> <i1 0, i1 0, i1 0> |
| 1739 |
Value *MixedTypeAnd = IRB.CreateSelect(Scalar, Vector, F); |
| 1740 |
|
| 1741 |
// select <3 x i1> Vector, <3 x i1> <i1 1, i1 1, i1 1>, <3 x i1> Vector |
| 1742 |
Value *VecOr = IRB.CreateSelect(Vector, T, Vector); |
| 1743 |
|
| 1744 |
// select i1 Scalar, <3 x i1> <i1 1, i1 1, i1 1>, <3 x i1> Vector |
| 1745 |
Value *MixedTypeOr = IRB.CreateSelect(Scalar, T, Vector); |
| 1746 |
|
| 1747 |
// We allow matching a real vector logical select, |
| 1748 |
// but not a scalar select of vector bools. |
| 1749 |
EXPECT_TRUE(match(VecAnd, m_LogicalAnd(m_Value(), m_Value()))); |
| 1750 |
EXPECT_FALSE(match(MixedTypeAnd, m_LogicalAnd(m_Value(), m_Value()))); |
| 1751 |
EXPECT_TRUE(match(VecOr, m_LogicalOr(m_Value(), m_Value()))); |
| 1752 |
EXPECT_FALSE(match(MixedTypeOr, m_LogicalOr(m_Value(), m_Value()))); |
| 1753 |
} |
| 1754 |
|
| 1755 |
TEST_F(PatternMatchTest, VScale) { |
| 1756 |
DataLayout DL = M->getDataLayout(); |
| 1757 |
|
| 1758 |
Type *VecTy = ScalableVectorType::get(IRB.getInt8Ty(), 1); |
| 1759 |
Type *VecPtrTy = VecTy->getPointerTo(); |
| 1760 |
Value *NullPtrVec = Constant::getNullValue(VecPtrTy); |
| 1761 |
Value *GEP = IRB.CreateGEP(VecTy, NullPtrVec, IRB.getInt64(1)); |
| 1762 |
Value *PtrToInt = IRB.CreatePtrToInt(GEP, DL.getIntPtrType(GEP->getType())); |
| 1763 |
EXPECT_TRUE(match(PtrToInt, m_VScale(DL))); |
| 1764 |
|
| 1765 |
// This used to cause assertion failures when attempting to match m_VScale. |
| 1766 |
// With opaque pointers the bitcast is no longer present. |
| 1767 |
Type *VecTy2 = ScalableVectorType::get(IRB.getInt8Ty(), 2); |
| 1768 |
Value *NullPtrVec2 = Constant::getNullValue(VecTy2->getPointerTo()); |
| 1769 |
Value *BitCast = IRB.CreateBitCast(NullPtrVec2, VecPtrTy); |
| 1770 |
Value *GEP2 = IRB.CreateGEP(VecTy, BitCast, IRB.getInt64(1)); |
| 1771 |
Value *PtrToInt2 = |
| 1772 |
IRB.CreatePtrToInt(GEP2, DL.getIntPtrType(GEP2->getType())); |
| 1773 |
EXPECT_TRUE(match(PtrToInt2, m_VScale(DL))); |
| 1774 |
} |
| 1775 |
|
| 1776 |
TEST_F(PatternMatchTest, NotForbidUndef) { |
| 1777 |
Type *ScalarTy = IRB.getInt8Ty(); |
| 1778 |
Type *VectorTy = FixedVectorType::get(ScalarTy, 3); |
| 1779 |
Constant *ScalarUndef = UndefValue::get(ScalarTy); |
| 1780 |
Constant *ScalarOnes = Constant::getAllOnesValue(ScalarTy); |
| 1781 |
Constant *VectorZero = Constant::getNullValue(VectorTy); |
| 1782 |
Constant *VectorOnes = Constant::getAllOnesValue(VectorTy); |
| 1783 |
|
| 1784 |
SmallVector<Constant *, 3> MixedElems; |
| 1785 |
MixedElems.push_back(ScalarOnes); |
| 1786 |
MixedElems.push_back(ScalarOnes); |
| 1787 |
MixedElems.push_back(ScalarUndef); |
| 1788 |
Constant *VectorMixed = ConstantVector::get(MixedElems); |
| 1789 |
|
| 1790 |
Value *Not = IRB.CreateXor(VectorZero, VectorOnes); |
| 1791 |
Value *X; |
| 1792 |
EXPECT_TRUE(match(Not, m_Not(m_Value()))); |
| 1793 |
EXPECT_TRUE(match(Not, m_NotForbidUndef(m_Value(X)))); |
| 1794 |
EXPECT_TRUE(match(X, m_Zero())); |
| 1795 |
|
| 1796 |
Value *NotCommute = IRB.CreateXor(VectorOnes, VectorZero); |
| 1797 |
Value *Y; |
| 1798 |
EXPECT_TRUE(match(NotCommute, m_Not(m_Value()))); |
| 1799 |
EXPECT_TRUE(match(NotCommute, m_NotForbidUndef(m_Value(Y)))); |
| 1800 |
EXPECT_TRUE(match(Y, m_Zero())); |
| 1801 |
|
| 1802 |
Value *NotWithUndefs = IRB.CreateXor(VectorZero, VectorMixed); |
| 1803 |
EXPECT_TRUE(match(NotWithUndefs, m_Not(m_Value()))); |
| 1804 |
EXPECT_FALSE(match(NotWithUndefs, m_NotForbidUndef(m_Value()))); |
| 1805 |
|
| 1806 |
Value *NotWithUndefsCommute = IRB.CreateXor(VectorMixed, VectorZero); |
| 1807 |
EXPECT_TRUE(match(NotWithUndefsCommute, m_Not(m_Value()))); |
| 1808 |
EXPECT_FALSE(match(NotWithUndefsCommute, m_NotForbidUndef(m_Value(X)))); |
| 1809 |
} |
| 1810 |
|
| 1811 |
template <typename T> struct MutableConstTest : PatternMatchTest { }; |
| 1812 |
|
| 1813 |
typedef ::testing::Types<std::tuple<Value*, Instruction*>, |
| 1814 |
std::tuple<const Value*, const Instruction *>> |
| 1815 |
MutableConstTestTypes; |
| 1816 |
TYPED_TEST_SUITE(MutableConstTest, MutableConstTestTypes, ); |
| 1817 |
|
| 1818 |
TYPED_TEST(MutableConstTest, ICmp) { |
| 1819 |
auto &IRB = PatternMatchTest::IRB; |
| 1820 |
|
| 1821 |
typedef std::tuple_element_t<0, TypeParam> ValueType; |
| 1822 |
typedef std::tuple_element_t<1, TypeParam> InstructionType; |
| 1823 |
|
| 1824 |
Value *L = IRB.getInt32(1); |
| 1825 |
Value *R = IRB.getInt32(2); |
| 1826 |
ICmpInst::Predicate Pred = ICmpInst::ICMP_UGT; |
| 1827 |
|
| 1828 |
ValueType MatchL; |
| 1829 |
ValueType MatchR; |
| 1830 |
ICmpInst::Predicate MatchPred; |
| 1831 |
|
| 1832 |
EXPECT_TRUE(m_ICmp(MatchPred, m_Value(MatchL), m_Value(MatchR)) |
| 1833 |
.match((InstructionType)IRB.CreateICmp(Pred, L, R))); |
| 1834 |
EXPECT_EQ(L, MatchL); |
| 1835 |
EXPECT_EQ(R, MatchR); |
| 1836 |
} |
| 1837 |
|
| 1838 |
TEST_F(PatternMatchTest, ConstExpr) { |
| 1839 |
Constant *G = |
| 1840 |
M->getOrInsertGlobal("dummy", PointerType::getUnqual(IRB.getInt32Ty())); |
| 1841 |
Constant *S = ConstantExpr::getPtrToInt(G, IRB.getInt32Ty()); |
| 1842 |
Type *VecTy = FixedVectorType::get(IRB.getInt32Ty(), 2); |
| 1843 |
PoisonValue *P = PoisonValue::get(VecTy); |
| 1844 |
Constant *V = ConstantExpr::getInsertElement(P, S, IRB.getInt32(0)); |
| 1845 |
|
| 1846 |
// The match succeeds on a constant that is a constant expression itself |
| 1847 |
// or a constant that contains a constant expression. |
| 1848 |
EXPECT_TRUE(match(S, m_ConstantExpr())); |
| 1849 |
EXPECT_TRUE(match(V, m_ConstantExpr())); |
| 1850 |
} |
| 1851 |
|
| 1852 |
} // anonymous namespace. |