blob: 0369ab3ad5f0e37a002c1d01df7802ed986b87d6 [file] [log] [blame]
Chris Lattner9f3c25a2009-11-09 22:57:59 +00001//===- InstructionSimplify.cpp - Fold instruction operands ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements routines for folding instructions into simpler forms
11// that do not require creating new instructions. For example, this does
12// constant folding, and can handle identities like (X&0)->0.
13//
14//===----------------------------------------------------------------------===//
15
16#include "llvm/Analysis/InstructionSimplify.h"
17#include "llvm/Analysis/ConstantFolding.h"
18#include "llvm/Instructions.h"
19using namespace llvm;
20
21
22/// SimplifyBinOp - Given operands for a BinaryOperator, see if we can
23/// fold the result. If not, this returns null.
24Value *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);
30 }
31 return 0;
32}
33
34
Chris Lattner9dbb4292009-11-09 23:28:39 +000035/// SimplifyICmpInst - Given operands for an ICmpInst, see if we can
36/// fold the result. If not, this returns null.
37Value *llvm::SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
38 const TargetData *TD) {
Chris Lattner9f3c25a2009-11-09 22:57:59 +000039 CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate;
Chris Lattner9dbb4292009-11-09 23:28:39 +000040 assert(CmpInst::isIntPredicate(Pred) && "Not an integer compare!");
Chris Lattner9f3c25a2009-11-09 22:57:59 +000041
42 if (Constant *CLHS = dyn_cast<Constant>(LHS))
Chris Lattner8f73dea2009-11-09 23:06:58 +000043 if (Constant *CRHS = dyn_cast<Constant>(RHS))
44 return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, TD);
Chris Lattner9f3c25a2009-11-09 22:57:59 +000045
46 // If this is an integer compare and the LHS and RHS are the same, fold it.
47 if (LHS == RHS)
Chris Lattner9dbb4292009-11-09 23:28:39 +000048 if (ICmpInst::isTrueWhenEqual(Pred))
49 return ConstantInt::getTrue(LHS->getContext());
50 else
51 return ConstantInt::getFalse(LHS->getContext());
52
Chris Lattner9f3c25a2009-11-09 22:57:59 +000053 return 0;
54}
55
Chris Lattner9dbb4292009-11-09 23:28:39 +000056/// SimplifyFCmpInst - Given operands for an FCmpInst, see if we can
57/// fold the result. If not, this returns null.
58Value *llvm::SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
59 const TargetData *TD) {
60 CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate;
61 assert(CmpInst::isFPPredicate(Pred) && "Not an FP compare!");
62
63 if (Constant *CLHS = dyn_cast<Constant>(LHS))
64 if (Constant *CRHS = dyn_cast<Constant>(RHS))
65 return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, TD);
66
67 return 0;
68}
69
70
71
72/// SimplifyCmpInst - Given operands for a CmpInst, see if we can
73/// fold the result.
74Value *llvm::SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
75 const TargetData *TD) {
76 if (CmpInst::isIntPredicate((CmpInst::Predicate)Predicate))
77 return SimplifyICmpInst(Predicate, LHS, RHS, TD);
78 return SimplifyFCmpInst(Predicate, LHS, RHS, TD);
79}
80