blob: 367a7d4bbfb24de8b42ae06dbfa7bba96c86722c [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
Chris Lattner210c5d42009-11-09 23:55:12 +000034static const Type *GetCompareTy(Value *Op) {
35 return CmpInst::makeCmpResultType(Op->getType());
36}
37
Chris Lattner9f3c25a2009-11-09 22:57:59 +000038
Chris Lattner9dbb4292009-11-09 23:28:39 +000039/// SimplifyICmpInst - Given operands for an ICmpInst, see if we can
40/// fold the result. If not, this returns null.
41Value *llvm::SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
42 const TargetData *TD) {
Chris Lattner9f3c25a2009-11-09 22:57:59 +000043 CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate;
Chris Lattner9dbb4292009-11-09 23:28:39 +000044 assert(CmpInst::isIntPredicate(Pred) && "Not an integer compare!");
Chris Lattner9f3c25a2009-11-09 22:57:59 +000045
46 if (Constant *CLHS = dyn_cast<Constant>(LHS))
Chris Lattner8f73dea2009-11-09 23:06:58 +000047 if (Constant *CRHS = dyn_cast<Constant>(RHS))
48 return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, TD);
Chris Lattner9f3c25a2009-11-09 22:57:59 +000049
Chris Lattner210c5d42009-11-09 23:55:12 +000050 // ITy - This is the return type of the compare we're considering.
51 const Type *ITy = GetCompareTy(LHS);
52
53 // icmp X, X -> true/false
Chris Lattner9f3c25a2009-11-09 22:57:59 +000054 if (LHS == RHS)
Chris Lattner210c5d42009-11-09 23:55:12 +000055 return ConstantInt::get(ITy, CmpInst::isTrueWhenEqual(Pred));
Chris Lattner9dbb4292009-11-09 23:28:39 +000056
Chris Lattner210c5d42009-11-09 23:55:12 +000057 // If we have a constant, make sure it is on the RHS.
58 if (isa<Constant>(LHS)) {
59 std::swap(LHS, RHS);
60 Pred = CmpInst::getSwappedPredicate(Pred);
61 }
62
63 if (isa<UndefValue>(RHS)) // X icmp undef -> undef
64 return UndefValue::get(ITy);
65
66 // icmp <global/alloca*/null>, <global/alloca*/null> - Global/Stack value
67 // addresses never equal each other! We already know that Op0 != Op1.
68 if ((isa<GlobalValue>(LHS) || isa<AllocaInst>(LHS) ||
69 isa<ConstantPointerNull>(LHS)) &&
70 (isa<GlobalValue>(RHS) || isa<AllocaInst>(RHS) ||
71 isa<ConstantPointerNull>(RHS)))
72 return ConstantInt::get(ITy, CmpInst::isFalseWhenEqual(Pred));
73
74 // See if we are doing a comparison with a constant.
75 if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
76 // If we have an icmp le or icmp ge instruction, turn it into the
77 // appropriate icmp lt or icmp gt instruction. This allows us to rely on
78 // them being folded in the code below.
79 switch (Pred) {
80 default: break;
81 case ICmpInst::ICMP_ULE:
82 if (CI->isMaxValue(false)) // A <=u MAX -> TRUE
83 return ConstantInt::getTrue(CI->getContext());
84 break;
85 case ICmpInst::ICMP_SLE:
86 if (CI->isMaxValue(true)) // A <=s MAX -> TRUE
87 return ConstantInt::getTrue(CI->getContext());
88 break;
89 case ICmpInst::ICMP_UGE:
90 if (CI->isMinValue(false)) // A >=u MIN -> TRUE
91 return ConstantInt::getTrue(CI->getContext());
92 break;
93 case ICmpInst::ICMP_SGE:
94 if (CI->isMinValue(true)) // A >=s MIN -> TRUE
95 return ConstantInt::getTrue(CI->getContext());
96 break;
97 }
98
99
100 }
101
102
Chris Lattner9f3c25a2009-11-09 22:57:59 +0000103 return 0;
104}
105
Chris Lattner9dbb4292009-11-09 23:28:39 +0000106/// SimplifyFCmpInst - Given operands for an FCmpInst, see if we can
107/// fold the result. If not, this returns null.
108Value *llvm::SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
109 const TargetData *TD) {
110 CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate;
111 assert(CmpInst::isFPPredicate(Pred) && "Not an FP compare!");
112
113 if (Constant *CLHS = dyn_cast<Constant>(LHS))
114 if (Constant *CRHS = dyn_cast<Constant>(RHS))
115 return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, TD);
116
Chris Lattner210c5d42009-11-09 23:55:12 +0000117 // Fold trivial predicates.
118 if (Pred == FCmpInst::FCMP_FALSE)
119 return ConstantInt::get(GetCompareTy(LHS), 0);
120 if (Pred == FCmpInst::FCMP_TRUE)
121 return ConstantInt::get(GetCompareTy(LHS), 1);
122
123 // If we have a constant, make sure it is on the RHS.
124 if (isa<Constant>(LHS)) {
125 std::swap(LHS, RHS);
126 Pred = CmpInst::getSwappedPredicate(Pred);
127 }
128
129 if (isa<UndefValue>(RHS)) // fcmp pred X, undef -> undef
130 return UndefValue::get(GetCompareTy(LHS));
131
132 // fcmp x,x -> true/false. Not all compares are foldable.
133 if (LHS == RHS) {
134 if (CmpInst::isTrueWhenEqual(Pred))
135 return ConstantInt::get(GetCompareTy(LHS), 1);
136 if (CmpInst::isFalseWhenEqual(Pred))
137 return ConstantInt::get(GetCompareTy(LHS), 0);
138 }
139
140 // Handle fcmp with constant RHS
141 if (Constant *RHSC = dyn_cast<Constant>(RHS)) {
142 // If the constant is a nan, see if we can fold the comparison based on it.
143 if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) {
144 if (CFP->getValueAPF().isNaN()) {
145 if (FCmpInst::isOrdered(Pred)) // True "if ordered and foo"
146 return ConstantInt::getFalse(CFP->getContext());
147 assert(FCmpInst::isUnordered(Pred) &&
148 "Comparison must be either ordered or unordered!");
149 // True if unordered.
150 return ConstantInt::getTrue(CFP->getContext());
151 }
152 }
153 }
154
Chris Lattner9dbb4292009-11-09 23:28:39 +0000155 return 0;
156}
157
158
159
160/// SimplifyCmpInst - Given operands for a CmpInst, see if we can
161/// fold the result.
162Value *llvm::SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
163 const TargetData *TD) {
164 if (CmpInst::isIntPredicate((CmpInst::Predicate)Predicate))
165 return SimplifyICmpInst(Predicate, LHS, RHS, TD);
166 return SimplifyFCmpInst(Predicate, LHS, RHS, TD);
167}
168