1 //===- InstructionSimplify.cpp - Fold instruction operands ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements routines for folding instructions into simpler forms
11 // that do not require creating new instructions. For example, this does
12 // constant folding, and can handle identities like (X&0)->0.
14 //===----------------------------------------------------------------------===//
16 #include "llvm/Analysis/InstructionSimplify.h"
17 #include "llvm/Analysis/ConstantFolding.h"
18 #include "llvm/Instructions.h"
22 /// SimplifyBinOp - Given operands for a BinaryOperator, see if we can
23 /// fold the result. If not, this returns null.
24 Value *llvm::SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
25 const TargetData *TD) {
26 if (Constant *CLHS = dyn_cast<Constant>(LHS))
27 if (Constant *CRHS = dyn_cast<Constant>(RHS)) {
28 Constant *COps[] = {CLHS, CRHS};
29 return ConstantFoldInstOperands(Opcode, LHS->getType(), COps, 2, TD);
35 /// SimplifyCompare - Given operands for a CmpInst, see if we can
37 Value *llvm::SimplifyCompare(unsigned Predicate, Value *LHS, Value *RHS,
38 const TargetData *TD) {
39 CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate;
41 if (Constant *CLHS = dyn_cast<Constant>(LHS))
42 if (Constant *CRHS = dyn_cast<Constant>(RHS)) {
43 Constant *COps[] = {CLHS, CRHS};
44 return ConstantFoldCompareInstOperands(Pred, COps, 2, TD);
47 // If this is an integer compare and the LHS and RHS are the same, fold it.
49 if (isa<IntegerType>(LHS->getType()) || isa<PointerType>(LHS->getType())) {
50 if (ICmpInst::isTrueWhenEqual(Pred))
51 return ConstantInt::getTrue(LHS->getContext());
53 return ConstantInt::getFalse(LHS->getContext());